WPF UserControl 上下文菜单可见性绑定 [英] WPF UserControl Context Menu Visibility Binding
问题描述
我的用户控件上下文菜单可见性无法绑定依赖属性.有什么想法吗?
My user control context menu visibility can't bind a dependency property. Any idea?
这是我的 WPF 代码
Here is my WPF code
<UserControl.ContextMenu>
<ContextMenu Visibility="{Binding ElementName=wellControl, Path=IsInCompactMode, Converter={StaticResource BooleanToVisibilityConverter}}">
<MenuItem Command="local:GCommands.Edit" />
<MenuItem Command="local:GCommands.Delete" />
<MenuItem Command="local:GCommands.ExportFcsFiles" />
<MenuItem Command="local:GCommands.BatchExportStatistics" />
<Separator/>
<MenuItem Command="local:GCommands.SaveAs" Header="Save As..." />
</ContextMenu>
</UserControl.ContextMenu>
如果我设置 Visibility="Hidden" 它将对我有用.像这样:
If I set Visibility="Hidden" it will work for me. like this:
<ContextMenu Visibility="Hidden"/>
如果用这个就不行了
<ContextMenu Visibility="{Binding ElementName=wellControl, Path=IsInCompactMode, Converter={StaticResource BooleanToVisibilityConverter}}">
我很确定 Visibility="{Binding ElementName=wellControl, Path=IsInCompactMode, Converter={StaticResource BooleanToVisibilityConverter}}" 没有问题,因为它适用于其他人.
I'm pretty sure Visibility="{Binding ElementName=wellControl, Path=IsInCompactMode, Converter={StaticResource BooleanToVisibilityConverter}}" has not problem, cuz it works for others.
这是我的依赖属性
public bool IsInCompactMode
{
get {return (bool)GetValue(IsInCompactModeProperty); }
set {SetValue(IsInCompactModeProperty, value); }
}
public static readonly DependencyProperty IsInCompactModeProperty =
DependencyProperty.Register("IsInCompactMode", typeof(bool), typeof(WellControl), new PropertyMetadata(false));
这个方法我试过了,好像还是不行,真是奇怪!!!
I tried this way, it seems it still doesn't work, this is really weird!!!
<ContextMenu x:Name="menu" IsOpen="{Binding ElementName=wellControl, Path=IsInCompactMode}">
我真的很困惑,怎么了?Binding ElementName=wellControl, Path=IsInCompactMode"适用于用户控件的其他部分,只是不适用于奇怪的上下文菜单?没有意义
I'm really confuse about it, what's wrong? "Binding ElementName=wellControl, Path=IsInCompactMode" works for other part of the user control, just doesn't work for the weird context menu? it doesn't make sence
推荐答案
1> ContextMenu、Popups、DataGridColumns 不是可视化树的一部分.所以使用 ElementName
或 RelativeSource
的绑定不会像那样工作.
1> ContextMenu, Popups, DataGridColumns are not part of visual tree. So the binding using ElementName
or RelativeSource
wont work just like that.
2> 如果您希望上下文菜单在特定情况下不显示,请使用触发器从视觉对象本身取消设置上下文菜单.
<TextBlock Text="ContextMenu is not shown when DataContext.IsShow is false"}">
<TextBlock.Style>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="ContextMenu"
Value="{StaticResource TextBlockContextMenu}" />
<Style.Triggers>
<DataTrigger Binding="{Binding IsShow}"
Value="False">
<Setter Property="ContextMenu"
Value="{x:Null}" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
3> 要将这些项目附加到可视化树,以便绑定起作用,我们使用代理元素方法...
3> To attach these items to the visual tree, so that binding works, we use the proxy element method...
我更喜欢第二步.
这篇关于WPF UserControl 上下文菜单可见性绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!