I want to try out a small, custom ValueConverter
in a class which after being instantiated (via default constructor, which only calls InitializeComponents()
), is given another DataContext
, specifically an instance of a ViewModel.
Using a StaticResource
within the Binding
does not work at all (yields a NullReferenceException
), since the DataContext
has since then been changed (is not this
anymore).
I've tried putting DataContext = this;
before the InitializeComponents
call, no change.
Should I be looking into this MarkupExtension
gizmo (as described in this article) ?
I've also tried creating an instance of the custom Value Converter
within the ViewModel (the current DataContext
), doesn't help either.
I can provide additional details at all times. Thank you in advance !
I'm trying to display a ContextMenu within the TextBlock. The ContextMenu contains a sole MenuItem. The header of the MenuItem can be "Settings" for instance. The Children (rendered as MenuItems as well) of the said MenuItem stem from an Enum, hence the ItemsSource on the MenuItem.
Now all is getting displayed nicely, yet I am trying to make one of the Children (e.g. a member of the Enum) to be selected per default, since there is already a default Setting. Further background info can be found in my other question.
Edit :
...
<UserControl.Resources>
<Helpers:DisplayTypeToDefaultValueConverter x:Key="displayTypeConverter" />
</UserControl.Resources>
...
<TextBlock x:Name="InstructionLabel"
TextWrapping="Wrap" Text="{Binding Path=SelectedNodeText}"
Grid.RowSpan="1">
<TextBlock.ContextMenu>
<ContextMenu>
<MenuItem Header="Settings" Name="SettingsPop"
DataContext="{Binding}"
ItemsSource="{Binding Source={StaticResource DisplayTypeValues}}"
IsCheckable="True"
Click="SettingsType_Click">
<MenuItem.ItemContainerStyle>
<Style TargetType="MenuItem">
<Setter Property="Header" Value="{Binding}"/>
<Setter Property="IsChecked">
<Setter.Value>
<Binding Converter="{StaticResource displayTypeConverter}" />
</Setter.Value>
</Setter>
</Style>
</MenuItem.ItemContainerStyle>
</ContextMenu>
</TextBlock>
I've only now realized that it's the dreaded ContextMenu. That's the problem, isn't it ?
Used an easier solution, as highlighted in my other related question.
Thank you for your input !