绑定更改后弹出WPF上下文菜单

时间:2017-01-11 14:57:12

标签: c# wpf contextmenu

我在UserControl中有一个Border控件,用作图形更改的开关。 Border有一个带有两个MenuItems的上下文菜单:" Open"和"关闭"。

当开关关闭时,仅打开"打开" MenuItem是可见的,当开关只打开"关闭"是可见的。对于某些开关,我需要完全禁用“打开”或“关闭”,因此我不希望它们随时可见。这是代码:

<Border.ContextMenu>
    <ContextMenu Name="switchContextMenu">
        <ContextMenu.Visibility>
            <MultiBinding Converter="{StaticResource ContextMenuBoolToVisibility}">
                <Binding Path="OpenAvailable" />
                <Binding Path="OpenVisible" />
                <Binding Path="CloseAvailable" />
                <Binding Path="CloseVisible" />
            </MultiBinding>
        </ContextMenu.Visibility>
        <MenuItem Name="miOpen" Header="{Binding Path=Resources.PowerControlSystem_OPEN, Source={StaticResource LocalizedStrings} }" 
                  Click="miOpen_Click">
            <MenuItem.Visibility>
                <MultiBinding Converter="{StaticResource BooleanToVisibilityMultiValueAND}">
                    <Binding Path="OpenAvailable" />
                    <Binding Path="OpenVisible" />
                </MultiBinding>
            </MenuItem.Visibility>
        </MenuItem>
        <MenuItem Name="miClose" Header="{Binding Path=Resources.PowerControlSystem_CLOSE, Source={StaticResource LocalizedStrings} }" 
                  Click="miClose_Click">
            <MenuItem.Visibility>
                <MultiBinding Converter="{StaticResource BooleanToVisibilityMultiValueAND}">
                    <Binding Path="CloseAvailable" />
                    <Binding Path="CloseVisible" />
                </MultiBinding>
            </MenuItem.Visibility>
        </MenuItem>
    </ContextMenu>
</Border.ContextMenu>

我将此开关用作通用组件,因此在另一个.xaml代码中,我设置了&#34; Open Available&#34;和&#34;关闭可用&#34;属性。这是我的问题:

我有一个Open Open属性设置为False的开关。默认情况下,此开关已打开。我可以关闭它,那没关系。然后,当我再次右键单击此开关时,上下文菜单不可见。这也没关系。

问题是,当我更改某些值时,此开关会再次打开,并且在那个时间点,会弹出“上下文菜单”。我认为问题出在Converter for ContextMenu可见性中。它在此时触发,ContextMenu变为可见。

有没有人知道如何阻止这种行为?

2 个答案:

答案 0 :(得分:0)

您可以设置ContextMenu

的样式,而不是设置Border可见性
<Border>
    <Border.Style>
        <Style TargetType="Border">
            <Setter Property="ContextMenu">
                <Setter.Value>
                    <ContextMenu Name="switchContextMenu">
                        <MenuItem Name="miOpen" Header="{Binding Path=Resources.PowerControlSystem_OPEN, Source={StaticResource LocalizedStrings} }" 
                            Click="miOpen_Click">
                            <MenuItem.Visibility>
                                <MultiBinding Converter="{StaticResource BooleanToVisibilityMultiValueAND}">
                                    <Binding Path="OpenAvailable" />
                                    <Binding Path="OpenVisible" />
                                </MultiBinding>
                            </MenuItem.Visibility>
                        </MenuItem>
                        <MenuItem Name="miClose" Header="{Binding Path=Resources.PowerControlSystem_CLOSE, Source={StaticResource LocalizedStrings} }" 
                            Click="miClose_Click">
                            <MenuItem.Visibility>
                                <MultiBinding Converter="{StaticResource BooleanToVisibilityMultiValueAND}">
                                    <Binding Path="CloseAvailable" />
                                    <Binding Path="CloseVisible" />
                                </MultiBinding>
                            </MenuItem.Visibility>
                        </MenuItem>
                    </ContextMenu>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <DataTrigger Value="False">
                    <DataTrigger.Binding>
                        <MultiBinding Converter="{StaticResource ContextMenuBoolAggregate}">
                            <Binding Path="OpenAvailable" />
                            <Binding Path="OpenVisible" />
                            <Binding Path="CloseAvailable" />
                            <Binding Path="CloseVisible" />
                        </MultiBinding>
                    </DataTrigger.Binding>
                    <Setter Property="ContextMenu" Value="{x:Null}"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Border.Style>
</Border>

因此触发器有条件地删除ContextMenu而不是更改其可见性。我使用ContextMenuBoolAggregate转换器名称来表示您需要创建一个新的转换器来确定正确的值以删除上下文菜单 - 我认为很清楚如何实现该部分。

如果您的条件足够简单,则可以MultiDataTrigger使用DataTrigger而不是MultiBindingConverter使用<MultiDataTrigger> <MultiDataTrigger.Conditions> <Condition Binding="{Binding OpenAvailable}" Value="False"/> <Condition Binding="{Binding OpenVisible}" Value="False"/> <Condition Binding="{Binding CloseAvailable}" Value="False"/> <Condition Binding="{Binding CloseVisible}" Value="False"/> </MultiDataTrigger.Conditions> <Setter Property="ContextMenu" Value="{x:Null}"/> </MultiDataTrigger>

range

答案 1 :(得分:0)

我以另一种方式找到了解决方案。我使用了ContextMenuService.IsEnabled属性。如果设置为false,则无法显示上下文菜单。所以,我在Border.xaml代码中添加了这个:

<ContextMenuService.IsEnabled>
    <MultiBinding Converter="{StaticResource ContextMenuBoolToVisibility}">
        <Binding Path="OpenAvailable" ElementName="mySwitch" />
        <Binding Path="OpenVisible" ElementName="mySwitch" />
        <Binding Path="CloseAvailable" ElementName="mySwitch" />
        <Binding Path="CloseVisible" ElementName="mySwitch" />
    </MultiBinding>
</ContextMenuService.IsEnabled>

我也改变了我的ContextMenuBoolToVisibility转换器。而不是返回Visibility.VisibleVisibility.Collapsed,现在为true属性返回falseContextMenuService.IsEnabled