Passing DataContext of ListBox to it's context menu<p>I have a question related to data binging. <br/><br/>How can I pass DataContext of ListView to the ListView's ContextMenu object  ? <br/><br/><br/>&lt;ListView IsSynchronizedWithCurrentItem=&quot;True&quot; Name=&quot;masterListView&quot;<br/>  ItemsSource=&quot;{Binding ImageCollectionView}&quot; Grid.Row=&quot;1&quot;&gt;<br/>    <br/>    &lt;ListView.Resources&gt;<br/>        &lt;ContextMenu x:Key=&quot;headerContextMenu&quot;&gt;<br/>            <br/>            <br/>            &lt;ContextMenu.DataContext&gt;<br/>             <strong>&lt;!-- I tried this binding but with no luck--&gt;<br/></strong>                &lt;Binding RelativeSource=&quot;{RelativeSource Mode=FindAncestor, AncestorType={x:Type ListView}}&quot;/&gt;<br/>            &lt;/ContextMenu.DataContext&gt;<br/>            <br/>            &lt;MenuItem Command=&quot;{Binding DoSomething}&quot;&gt;<br/>                &lt;MenuItem.Header&gt;<br/>                    &lt;cp:GVColumnSelectorControl&gt;&lt;/cp:GVColumnSelectorControl&gt;<br/>                &lt;/MenuItem.Header&gt;<br/>            &lt;/MenuItem&gt;<br/>            <br/>        &lt;/ContextMenu&gt;<br/>    &lt;/ListView.Resources&gt;<br/>    <br/><br/>    &lt;ListView.GroupStyle&gt;<br/>          &lt;!-- commented out for clarity --&gt;<br/>    &lt;/ListView.GroupStyle&gt;<br/>   </p> <p>    <br/>    &lt;ListView.View&gt;<br/>        &lt;GridView&gt;<br/>            <br/>            &lt;GridView.ColumnHeaderContainerStyle&gt;<br/>                &lt;Style TargetType=&quot;{x:Type GridViewColumnHeader}&quot;&gt;<br/>                    &lt;Setter Property=&quot;ContextMenu&quot; Value=&quot;{StaticResource <strong>headerContextMenu</strong>}&quot; /&gt;<br/>                &lt;/Style&gt;<br/>            &lt;/GridView.ColumnHeaderContainerStyle&gt;<br/>            <br/>            &lt;GridViewColumn Width=&quot;100&quot; Header=&quot;Property 1&quot; DisplayMemberBinding=&quot;{Binding SomeProperty1}&quot;&gt;&lt;/GridViewColumn&gt;<br/>            &lt;GridViewColumn Width=&quot;100&quot; Header=&quot;Property 2&quot; DisplayMemberBinding=&quot;{Binding SomeProperty2}&quot;&gt;&lt;/GridViewColumn&gt;<br/>            &lt;GridViewColumn Width=&quot;100&quot; Header=&quot;Property 3&quot; DisplayMemberBinding=&quot;{Binding SomeProperty3}&quot;&gt;&lt;/GridViewColumn&gt;<br/>        &lt;/GridView&gt;<br/>    &lt;/ListView.View&gt;<br/>    <br/>&lt;/ListView&gt;<br/><br/><br/><br/>Context menu has to display a UserControl that needs to have access to the same object as the one bound to the ListView <br/><br/><br/><br/><br/>Thanks in advance for help,<br/>Regards,<br/>Michal</p>© 2009 Microsoft Corporation. All rights reserved.Thu, 09 Jul 2009 22:13:20 Z6121b651-5250-4762-934b-ddc55ee2a9d4http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/6121b651-5250-4762-934b-ddc55ee2a9d4#6121b651-5250-4762-934b-ddc55ee2a9d4http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/6121b651-5250-4762-934b-ddc55ee2a9d4#6121b651-5250-4762-934b-ddc55ee2a9d4Michał Januszczykhttp://social.msdn.microsoft.com/Profile/en-US/?user=Micha%u0142%20JanuszczykPassing DataContext of ListBox to it's context menu<p>I have a question related to data binging. <br/><br/>How can I pass DataContext of ListView to the ListView's ContextMenu object  ? <br/><br/><br/>&lt;ListView IsSynchronizedWithCurrentItem=&quot;True&quot; Name=&quot;masterListView&quot;<br/>  ItemsSource=&quot;{Binding ImageCollectionView}&quot; Grid.Row=&quot;1&quot;&gt;<br/>    <br/>    &lt;ListView.Resources&gt;<br/>        &lt;ContextMenu x:Key=&quot;headerContextMenu&quot;&gt;<br/>            <br/>            <br/>            &lt;ContextMenu.DataContext&gt;<br/>             <strong>&lt;!-- I tried this binding but with no luck--&gt;<br/></strong>                &lt;Binding RelativeSource=&quot;{RelativeSource Mode=FindAncestor, AncestorType={x:Type ListView}}&quot;/&gt;<br/>            &lt;/ContextMenu.DataContext&gt;<br/>            <br/>            &lt;MenuItem Command=&quot;{Binding DoSomething}&quot;&gt;<br/>                &lt;MenuItem.Header&gt;<br/>                    &lt;cp:GVColumnSelectorControl&gt;&lt;/cp:GVColumnSelectorControl&gt;<br/>                &lt;/MenuItem.Header&gt;<br/>            &lt;/MenuItem&gt;<br/>            <br/>        &lt;/ContextMenu&gt;<br/>    &lt;/ListView.Resources&gt;<br/>    <br/><br/>    &lt;ListView.GroupStyle&gt;<br/>          &lt;!-- commented out for clarity --&gt;<br/>    &lt;/ListView.GroupStyle&gt;<br/>   </p> <p>    <br/>    &lt;ListView.View&gt;<br/>        &lt;GridView&gt;<br/>            <br/>            &lt;GridView.ColumnHeaderContainerStyle&gt;<br/>                &lt;Style TargetType=&quot;{x:Type GridViewColumnHeader}&quot;&gt;<br/>                    &lt;Setter Property=&quot;ContextMenu&quot; Value=&quot;{StaticResource <strong>headerContextMenu</strong>}&quot; /&gt;<br/>                &lt;/Style&gt;<br/>            &lt;/GridView.ColumnHeaderContainerStyle&gt;<br/>            <br/>            &lt;GridViewColumn Width=&quot;100&quot; Header=&quot;Property 1&quot; DisplayMemberBinding=&quot;{Binding SomeProperty1}&quot;&gt;&lt;/GridViewColumn&gt;<br/>            &lt;GridViewColumn Width=&quot;100&quot; Header=&quot;Property 2&quot; DisplayMemberBinding=&quot;{Binding SomeProperty2}&quot;&gt;&lt;/GridViewColumn&gt;<br/>            &lt;GridViewColumn Width=&quot;100&quot; Header=&quot;Property 3&quot; DisplayMemberBinding=&quot;{Binding SomeProperty3}&quot;&gt;&lt;/GridViewColumn&gt;<br/>        &lt;/GridView&gt;<br/>    &lt;/ListView.View&gt;<br/>    <br/>&lt;/ListView&gt;<br/><br/><br/><br/>Context menu has to display a UserControl that needs to have access to the same object as the one bound to the ListView <br/><br/><br/><br/><br/>Thanks in advance for help,<br/>Regards,<br/>Michal</p>Thu, 02 Jul 2009 00:13:48 Z2009-07-02T00:13:48Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/6121b651-5250-4762-934b-ddc55ee2a9d4#4d0b6d44-65e5-4643-bf5d-742b2ef2fa13http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/6121b651-5250-4762-934b-ddc55ee2a9d4#4d0b6d44-65e5-4643-bf5d-742b2ef2fa13Mariano O. Rodriguezhttp://social.msdn.microsoft.com/Profile/en-US/?user=Mariano%20O.%20RodriguezPassing DataContext of ListBox to it's context menuTry:<br/><br/>&lt;Binding<br/> RelativeSource=&quot;{RelativeSource Mode=FindAncestor, AncestorType={x:Type ListView}}&quot;<br/> <strong>Path=&quot;DataContext&quot;</strong> /&gt;<br/><br/><br/>It should be work.Thu, 02 Jul 2009 00:20:28 Z2009-07-02T00:20:59Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/6121b651-5250-4762-934b-ddc55ee2a9d4#a36460e4-aa5a-47ff-b242-cc1d15200e2bhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/6121b651-5250-4762-934b-ddc55ee2a9d4#a36460e4-aa5a-47ff-b242-cc1d15200e2bMichał Januszczykhttp://social.msdn.microsoft.com/Profile/en-US/?user=Micha%u0142%20JanuszczykPassing DataContext of ListBox to it's context menuStill does not work. <br/>I can see that the DataContext property is still null <br/><br/>Thanks for reply<br/>MichalThu, 02 Jul 2009 00:31:56 Z2009-07-02T00:31:56Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/6121b651-5250-4762-934b-ddc55ee2a9d4#5e3b0d37-52f4-49f6-a80a-04c3f474487ahttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/6121b651-5250-4762-934b-ddc55ee2a9d4#5e3b0d37-52f4-49f6-a80a-04c3f474487aMichał Januszczykhttp://social.msdn.microsoft.com/Profile/en-US/?user=Micha%u0142%20JanuszczykPassing DataContext of ListBox to it's context menuI'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:<br/><br/> <pre>&lt;MenuItem&gt; &lt;!-- to be removed--&gt; &lt;MenuItem.Header&gt; &lt;TextBlock Grid.Row=&quot;2&quot; Text=&quot;{Binding}&quot;&gt;&lt;/TextBlock&gt; &lt;/MenuItem.Header&gt; &lt;/MenuItem&gt;</pre> <br/><br/><br/>The problem is now how to pass this DataContext from menuitem (which has it for sure) into <br/>&lt;cp:GVColumnSelectorControl&gt;&lt;/cp:GVColumnSelectorControl&gt;<br/><br/>I can see in the codebehin that DataContext property is null. <br/><br/>I tried to define two DependencyProperties: one of type int, and the second of BusinessType. <br/>When the control is declared as follows: <br/> <pre><pre>&lt;MenuItem Command=&quot;{Binding DoSomething}&quot;&gt; &lt;MenuItem.Header&gt; &lt;cp:GVColumnSelectorControl IntProperty=&quot;7&quot; BusinessTypeProperty=&quot;{Binding}&quot;&gt;&lt;/cp:GVColumnSelectorControl&gt; &lt;/MenuItem.Header&gt; &lt;/MenuItem&gt; </pre> <br/><br/></pre> <br/>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] <br/><br/><br/>Thanks, <br/>Michal<br/><br/>Thu, 02 Jul 2009 23:15:24 Z2009-07-02T23:15:24Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/6121b651-5250-4762-934b-ddc55ee2a9d4#1ccf0ca8-3fb9-4848-bd0d-76a57307e108http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/6121b651-5250-4762-934b-ddc55ee2a9d4#1ccf0ca8-3fb9-4848-bd0d-76a57307e108Mark Salsberyhttp://social.msdn.microsoft.com/Profile/en-US/?user=Mark%20SalsberyPassing DataContext of ListBox to it's context menu<blockquote>I can see in codebehind that IntProperty is 7, but BusinesstypeProperty value is null</blockquote> <br/><br/>Do you need to set the binding mode to OneWayToSource or TwoWay?<br/><br/><br/>Mark<br/><hr class="sig"> Mark Salsbery Microsoft MVP - Visual C++ Thu, 02 Jul 2009 23:30:41 Z2009-07-02T23:30:41Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/6121b651-5250-4762-934b-ddc55ee2a9d4#68a00955-949b-4878-9c35-d17d8de365dahttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/6121b651-5250-4762-934b-ddc55ee2a9d4#68a00955-949b-4878-9c35-d17d8de365daMichał Januszczykhttp://social.msdn.microsoft.com/Profile/en-US/?user=Micha%u0142%20JanuszczykPassing DataContext of ListBox to it's context menuOne way would be enough<br/><br/><br/>Regards,<br/>MichalThu, 02 Jul 2009 23:33:18 Z2009-07-02T23:33:18Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/6121b651-5250-4762-934b-ddc55ee2a9d4#a494c2c3-197f-4a67-8cb0-185a37d5e453http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/6121b651-5250-4762-934b-ddc55ee2a9d4#a494c2c3-197f-4a67-8cb0-185a37d5e453Mark Salsberyhttp://social.msdn.microsoft.com/Profile/en-US/?user=Mark%20SalsberyPassing DataContext of ListBox to it's context menu<blockquote>One way would be enough<br/><br/><br/>Regards,<br/>Michal</blockquote> <br/>Wow I was thinking totally backward on that one, sorry....disregard.<br/><br/><br/>Anyway, maybe try<br/><br/> <pre lang=x-xml>&lt;cp:GVColumnSelectorControl IntProperty=&quot;7&quot; BusinessTypeProperty=&quot;{Binding Path=DataContext, RelativeSource={RelativeSource self}}&quot;&gt;&lt;/cp:GVColumnSelectorControl&gt; </pre> <br/><br/> <hr class=sig> Mark Salsbery Microsoft MVP - Visual C++Fri, 03 Jul 2009 00:51:04 Z2009-07-03T00:54:01Z