Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to hide the Separator in a context menu using MultiBinding?
    primarykey
    data
    text
    <p>I am using a context menu on a wpf treeview and I am pretty much there as to what I want. Before I explain the problem let me explain what the XAML definition for the context menu is doing.</p> <p>For each menu item in the context menu we have a command that either disables or enables the menu item based on the commands CanExecute method. Each command will set the IsEnabled property of the corresponding menu item depending on the result of CanExecute.</p> <p>IsEnabled for each the menu item is bound to a BooleanToVisibilityConverter which converts the the IsEnabled bool value to a Collapse or Visible value to be bound the Visibility propery of the menu item. This again works fine and my menu items are displaying and hiding fine.</p> <p>Now for the problem. In the XAML below we have two menu items(addCategoryMenuItem and removeCategoryMenuItem) above a separator. I am trying to MultiBinding to the IsEnabled property of these two menu items via a custom implementation of IMultiValueConverter (MultiBooleanToVisibilityConverter) so that when the two menu items are disabled I can set the Visibility property of the Separator to collapsed and hence hide the separator when the menu items are disabled.</p> <p>For the Convert method in my Converter(MultiBooleanToVisibilityConverter) the parameter value (object [] values) I get two items in the array that hold the value "{DependencyProperty.UnsetValue}". These cannot be cast to boolean values and hence my Visibility value cannot be worked out.</p> <p>Maybe is has something do with ElementName used in the MultiBinding. Can it not find the element? I have tried using RelativeSource i.e find ancestor etc. But I just got confused. I have spent hours on this so now I leave it to the community.</p> <p>Kind regards</p> <p>Mohammad</p> <pre><code>&lt;ContextMenu x:Key="CategoryMenu"&gt; &lt;ContextMenu.ItemContainerStyle&gt; &lt;Style TargetType="{x:Type Control}"&gt; &lt;Setter Property="Visibility" Value="{Binding Path=IsEnabled, RelativeSource={RelativeSource Self}, Mode=OneWay, Converter={StaticResource booleanToVisibilityConverter}}" /&gt; &lt;/Style&gt; &lt;/ContextMenu.ItemContainerStyle&gt; &lt;ContextMenu.Items&gt; &lt;MenuItem x:Name="addCategoryMenuItem" Header="add category" Command="{Binding AddCategory}"&gt; &lt;MenuItem.Icon&gt; &lt;Image Source="/Images/add.png" Width="16" Height="16" /&gt; &lt;/MenuItem.Icon&gt; &lt;/MenuItem&gt; &lt;MenuItem x:Name="removeCategoryMenuItem" Header="remove category" Command="{Binding RemoveCategory}"&gt; &lt;MenuItem.Icon&gt; &lt;Image Source="/Images/remove.png" Width="16" Height="16" /&gt; &lt;/MenuItem.Icon&gt; &lt;/MenuItem&gt; &lt;Separator&gt; &lt;Separator.Visibility&gt; &lt;MultiBinding Converter="{StaticResource multiBooleanToVisibilityConverter}"&gt; &lt;Binding Mode="OneWay" ElementName="addCategoryMenuItem" Path="IsEnabled" /&gt; &lt;Binding Mode="OneWay" ElementName="removeCategoryMenuItem" Path="IsEnabled" /&gt; &lt;/MultiBinding&gt; &lt;/Separator.Visibility&gt; &lt;/Separator&gt; &lt;MenuItem x:Name="refreshCategoryMenuItem" Header="refresh" Command="{Binding RefreshCategory}"&gt; &lt;MenuItem.Icon&gt; &lt;Image Source="/Images/refresh.png" Width="16" Height="16" /&gt; &lt;/MenuItem.Icon&gt; &lt;/MenuItem&gt; &lt;/ContextMenu.Items&gt; &lt;/ContextMenu&gt; </code></pre>
    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