Note that there are some explanatory texts on larger screens.

plurals
  1. POBind ContextMenu's MenuItem visibility to ListView selection
    primarykey
    data
    text
    <p>I have a user control with a ListView containing simple items from an ObservableCollection. I would like the ContextMenu of that ListView to contain items depending on what's selected in the ListView. If no item is selected, some MenuItems should not be visible. </p> <p>My converter isn't even called when I open the ContextMenu. The binding seems to be wrong, I find this in the output window:</p> <blockquote> <p>System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=listView'. BindingExpression:Path=SelectedItem; DataItem=null; target element is 'MenuItem' (Name=''); target property is 'Visibility' (type 'Visibility')</p> </blockquote> <p>I don't understand what's wrong and could not figure it out by searching the web.</p> <p>Here is some simplified code:</p> <pre><code>&lt;UserControl x:Class="MyApp.DatabaseControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:l="clr-namespace:MyApp" Height="Auto" Width="Auto"&gt; &lt;UserControl.Resources&gt; &lt;l:ValueToVisibilityConverter x:Key="valueToVisibility" /&gt; &lt;/UserControl.Resources&gt; &lt;Grid&gt; &lt;ListView x:Name="listView" ItemsSource="{Binding Persons}"&gt; &lt;ListView.View&gt; &lt;GridView&gt; &lt;GridViewColumn Width="140" Header="First Name" DisplayMemberBinding="{Binding FirstName}"/&gt; &lt;GridViewColumn Width="140" Header="Last Name" DisplayMemberBinding="{Binding LastName}" /&gt; &lt;/GridView&gt; &lt;/ListView.View&gt; &lt;ListView.ContextMenu&gt; &lt;ContextMenu&gt; &lt;MenuItem Header="Open" Visibility="{Binding SelectedItem, ElementName=listView, Converter={StaticResource valueToVisibility}}"/&gt; &lt;Separator/&gt; &lt;MenuItem Header="Add..."/&gt; &lt;MenuItem Header="Remove"/&gt; &lt;/ContextMenu&gt; &lt;/ListView.ContextMenu&gt; &lt;/ListView&gt; &lt;/Grid&gt; </code></pre> <p></p> <p>Thanks a lot!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload