Passing DataContext of ListBox to it's context menu
I have a question related to data binging.
How can I pass DataContext of ListView to the ListView's ContextMenu object ?
<ListView IsSynchronizedWithCurrentItem="True" Name="masterListView"
ItemsSource="{Binding ImageCollectionView}" Grid.Row="1">
<ListView.Resources>
<ContextMenu x:Key="headerContextMenu">
<ContextMenu.DataContext>
<!-- I tried this binding but with no luck-->
<Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type ListView}}"/>
</ContextMenu.DataContext>
<MenuItem Command="{Binding DoSomething}">
<MenuItem.Header>
<cp:GVColumnSelectorControl></cp:GVColumnSelectorControl>
</MenuItem.Header>
</MenuItem>
</ContextMenu>
</ListView.Resources>
<ListView.GroupStyle>
<!-- commented out for clarity -->
</ListView.GroupStyle>
<ListView.View>
<GridView>
<GridView.ColumnHeaderContainerStyle>
<Style TargetType="{x:Type GridViewColumnHeader}">
<Setter Property="ContextMenu" Value="{StaticResource headerContextMenu}" />
</Style>
</GridView.ColumnHeaderContainerStyle>
<GridViewColumn Width="100" Header="Property 1" DisplayMemberBinding="{Binding SomeProperty1}"></GridViewColumn>
<GridViewColumn Width="100" Header="Property 2" DisplayMemberBinding="{Binding SomeProperty2}"></GridViewColumn>
<GridViewColumn Width="100" Header="Property 3" DisplayMemberBinding="{Binding SomeProperty3}"></GridViewColumn>
</GridView>
</ListView.View>
</ListView>
Context menu has to display a UserControl that needs to have access to the same object as the one bound to the ListView
Thanks in advance for help,
Regards,
Michal
すべての返信
- Try:
<Binding
RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type ListView}}"
Path="DataContext" />
It should be work. - Still does not work.
I can see that the DataContext property is still null
Thanks for reply
Michal - I've found that when I remove wrongly specified binding, Context menu automatically inherits DataContext properly. I added the second menuitem for testing purposes and it correctly prints dataContext type:
<MenuItem> <!-- to be removed--> <MenuItem.Header> <TextBlock Grid.Row="2" Text="{Binding}"></TextBlock> </MenuItem.Header> </MenuItem>
The problem is now how to pass this DataContext from menuitem (which has it for sure) into
<cp:GVColumnSelectorControl></cp:GVColumnSelectorControl>
I can see in the codebehin that DataContext property is null.
I tried to define two DependencyProperties: one of type int, and the second of BusinessType.
When the control is declared as follows:
<MenuItem Command="{Binding DoSomething}"> <MenuItem.Header> <cp:GVColumnSelectorControl IntProperty="7" BusinessTypeProperty="{Binding}"></cp:GVColumnSelectorControl> </MenuItem.Header> </MenuItem>
I can see in codebehind that IntProperty is 7, but BusinesstypeProperty value is null :-( [note that MenuItem's DataContext is set with the data for sure]
Thanks,
Michal I can see in codebehind that IntProperty is 7, but BusinesstypeProperty value is null
Do you need to set the binding mode to OneWayToSource or TwoWay?
Mark
Mark Salsbery Microsoft MVP - Visual C++- One way would be enough
Regards,
Michal One way would be enough
Regards,
Michal
Wow I was thinking totally backward on that one, sorry....disregard.
Anyway, maybe try
<cp:GVColumnSelectorControl IntProperty="7" BusinessTypeProperty="{Binding Path=DataContext, RelativeSource={RelativeSource self}}"></cp:GVColumnSelectorControl>
Mark Salsbery Microsoft MVP - Visual C++- 回答の候補に設定Hua ChenMSFT, モデレータ2009年7月8日 6:58

