0

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 ?

Community
  • 1
  • 1
Dr1Ku
  • 2,875
  • 3
  • 47
  • 56
  • 1
    1. DataContext has no influence on StaticResource binding. 2. Can you please provide the line of xaml where you are binding? – alpha-mouse Dec 01 '10 at 15:19
  • And where is DisplayTypeValues? Can't see the definition. And code of your DisplayTypeToDefaultValueConverter would be helpful too. – The Smallest Dec 01 '10 at 15:31
  • Did you tried to set breakpoint in your CustomConverter and see if this code is reached? – The Smallest Dec 01 '10 at 15:53
  • @The_Smallest Thanks for the input, I did try to set a breakpoint, doesn't reach that far. The Converter doesn't do anything in particular, I only want it to reach the code, I'll see from there. – Dr1Ku Dec 01 '10 at 16:54

3 Answers3

1

The DataContext inside the ItemContainerStyle is a member of the DisplayTypeValues collection. The only thing in the XAML you posted that will be affected by the DataContext of the UserControl changing is the InstructionLabel's text. Setting DataContext="{Binding}" as you're doing on the MenuItem is also redundant as the value will already be inherited from the parent ContextMenu.

It's not clear from your question or code what you're expecting from the DataContext or what you're trying to do with it.

John Bowen
  • 24,213
  • 4
  • 58
  • 56
  • Ok, thank you for the input, I am trying to get a ContextMenu (available per right-click) to show up within the TextBlock. The ContextMenu consists of a MenuItem, the (child) MenuItem gets its values from an Enum. What I am trying to achieve is having one item of the (child) MenuItem checked per default. – Dr1Ku Dec 01 '10 at 16:57
0

Just several thoughts:

  1. Are you sure you didn't miss setting binding path in <Binding Converter="{StaticResource displayTypeConverter}" />?
  2. Did you check the StackTrace of the thrown exception and all it's InnerExceptions to see, whether there was something interesting?
alpha-mouse
  • 4,953
  • 24
  • 36
  • I think I'm missing the Path, should that be "IsChecked" ? The Exception is shown within a ridiculous MessageBox, setting the "WPF Data Binding" debug level higher from VS Preferences didn't help as well. – Dr1Ku Dec 01 '10 at 16:59
  • @Dr1Ku: If I understand you correctly then you can press "View Detail..." link in the bottom of the exception dialog box to see details of the exception. I have no more thoughts – alpha-mouse Dec 01 '10 at 17:17
0

Used an easier solution, as highlighted in my other related question.
Thank you for your input !

Community
  • 1
  • 1
Dr1Ku
  • 2,875
  • 3
  • 47
  • 56