如何使用MultiBinding在上下文菜单中隐藏分隔符?

时间:2011-02-19 07:53:31

标签: wpf contextmenu hide separator multibinding

我在wpf树视图上使用上下文菜单,我几乎就是我想要的。在我解释这个问题之前,让我解释一下上下文菜单的XAML定义是什么。

对于上下文菜单中的每个菜单项,我们都有一个命令,可以根据命令CanExecute方法禁用或启用菜单项。每个命令都将根据CanExecute的结果设置相应菜单项的IsEnabled属性。

每个菜单项的IsEnabled都绑定到BooleanToVisibilityConverter,后者将IsEnabled bool值转换为Collapse或Visible值,以绑定菜单项的Visibility属性。这再次正常,我的菜单项显示和隐藏得很好。

现在出现问题。在下面的XAML中,我们在分隔符上方有两个菜单项(addCategoryMenuItem和removeCategoryMenuItem)。我试图通过IMultiValueConverter(MultiBooleanToVisibilityConverter)的自定义实现MultiBinding到这两个菜单项的IsEnabled属性,这样当两个菜单项被禁用时我可以将Separator的Visibility属性设置为折叠,从而隐藏分隔符时菜单项已禁用。

对于我的Converter(MultiBooleanToVisibilityConverter)中的Convert方法,参数值(object [] values)我在数组中得到两个包含值“{DependencyProperty.UnsetValue}”的项。这些不能转换为布尔值,因此我的Visibility值无法计算出来。

可能与MultiBinding中使用的ElementName有关。它能找不到元素吗?我尝试过使用RelativeSource,即找到祖先等等。但我只是感到困惑。我花了好几个小时,所以现在把它留给社区。

亲切的问候

穆罕默德

<ContextMenu x:Key="CategoryMenu">
    <ContextMenu.ItemContainerStyle>
        <Style TargetType="{x:Type Control}">
            <Setter Property="Visibility" Value="{Binding Path=IsEnabled, RelativeSource={RelativeSource Self}, Mode=OneWay, Converter={StaticResource booleanToVisibilityConverter}}" />
        </Style>
    </ContextMenu.ItemContainerStyle>
    <ContextMenu.Items>
        <MenuItem x:Name="addCategoryMenuItem" Header="add category" Command="{Binding AddCategory}">
            <MenuItem.Icon>
                <Image Source="/Images/add.png" Width="16" Height="16" />
            </MenuItem.Icon>
        </MenuItem>
        <MenuItem x:Name="removeCategoryMenuItem" Header="remove category" Command="{Binding RemoveCategory}">
            <MenuItem.Icon>
                <Image Source="/Images/remove.png" Width="16" Height="16" />
            </MenuItem.Icon>
        </MenuItem>
        <Separator>
            <Separator.Visibility>
                <MultiBinding Converter="{StaticResource multiBooleanToVisibilityConverter}">
                    <Binding Mode="OneWay" ElementName="addCategoryMenuItem" Path="IsEnabled" />
                    <Binding Mode="OneWay" ElementName="removeCategoryMenuItem" Path="IsEnabled" />
                </MultiBinding>
            </Separator.Visibility>
        </Separator>
        <MenuItem x:Name="refreshCategoryMenuItem" Header="refresh" Command="{Binding RefreshCategory}">
            <MenuItem.Icon>
                <Image Source="/Images/refresh.png" Width="16" Height="16" />
            </MenuItem.Icon>
        </MenuItem>
    </ContextMenu.Items>
</ContextMenu>

2 个答案:

答案 0 :(得分:2)

好的,经过一段时间的休息后,我设法解决了这个问题。我不得不使用RelativeSource和FindAncestor来获取上下文菜单对象,然后访问items集合,然后使用索引器值来获取菜单项。我认为如果我可以使用菜单项名称会更好,因为我不喜欢我的代码或xaml中的魔术数字。

<Separator>
    <Separator.Visibility>
        <MultiBinding Converter="{StaticResource multiBooleanToVisibilityConverter}">
            <Binding Mode="OneWay" RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}" Path="Items[0].IsEnabled" />
            <Binding Mode="OneWay" RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}" Path="Items[1].IsEnabled" />
        </MultiBinding>
    </Separator.Visibility>
</Separator>

答案 1 :(得分:1)

我扩展了普通分隔符以创建一个分隔符,根据父ItemsControl中的其他项自动确定它是否应该显示。

public class AutoVisibilitySeparator : Separator
{
    public AutoVisibilitySeparator()
    {
        if (DesignerProperties.GetIsInDesignMode(this))
            return;

        Visibility = Visibility.Collapsed; // Starting collapsed so we don't see them disappearing

        Loaded += OnLoaded;
    }

    private void OnLoaded(object sender, RoutedEventArgs e)
    {
        // We have to wait for all siblings to update their visibility before we update ours.
        // This is the best way I've found yet. I tried waiting for the context menu opening or visibility changed, on render and lots of other events
        Dispatcher.BeginInvoke(new Action(UpdateVisibility), DispatcherPriority.Render);
    }

    private void UpdateVisibility()
    {
        var showSeparator = false;

        // Go through each sibling of the parent context menu looking for a visible item before and after this separator
        var foundThis = false;
        var foundItemBeforeThis = false;
        foreach (var visibleItem in ((ItemsControl)Parent).Items.OfType<UIElement>().Where(i => i.Visibility == Visibility.Visible || i == this))
        {
            if (visibleItem == this)
            {
                // If there were no visible items prior to this separator then we hide it
                if (!foundItemBeforeThis)
                    break;

                foundThis = true;
            }
            else if (visibleItem is AutoVisibilitySeparator || visibleItem is Separator)
            {
                // If we already found this separator and this next item is not a visible item we hide this separator
                if (foundThis)
                    break;

                foundItemBeforeThis = false; // The current item is a separator so we reset the search for an item
            }
            else
            {
                if (foundThis)
                {
                    // We found a visible item after finding this separator so we're done and should show this
                    showSeparator = true;
                    break;
                }

                foundItemBeforeThis = true;
            }
        }

        Visibility = showSeparator ? Visibility.Visible : Visibility.Collapsed;
    }
}