是否有可能使用可检查的菜单项为单选按钮控件模板?
-
19-08-2019 - |
题
我在我的与像一个单选按钮组的多个选项WPF应用程序的菜单(选择一个取消选择它们中的其余部分)。我想用辨认的菜单项作为单选按钮模板。
我试过设置模板,但它似乎没有工作像预期。似乎选择和取消选择项不与单选按钮值同步。
我想我可以使用路径或东西使用更复杂的模板和“假”所选择的标记,但它似乎非常大量的工作,对于这样一个简单的目的。还使用更复杂的模板时,我必须解决,我宁愿不希望做不同的主题。
下面是一个简单的例子来说明该问题。
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Page.Resources>
<ControlTemplate x:Key="Template" TargetType="{x:Type RadioButton}">
<MenuItem x:Name="item" Header="{TemplateBinding Content}" IsCheckable="True" IsChecked="False" />
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter TargetName="item" Property="IsChecked" Value="True" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Page.Resources>
<StackPanel>
<RadioButton Content="Foo" Template="{StaticResource Template}"/>
<RadioButton Content="Bar" Template="{StaticResource Template}"/>
<RadioButton Content="Biz" Template="{StaticResource Template}"/>
</StackPanel>
</Page>
解决方案
的问题似乎是,所述MenuItem
的鼠标事件处理程序被接管的RadioButton
。当我在IsHitTestVisible
设置MenuItem
为错误并添加了Border
来吸收鼠标事件,它似乎像您期望的工作:
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Page.Resources>
<ControlTemplate x:Key="Template" TargetType="{x:Type RadioButton}">
<Border Background="Transparent">
<MenuItem Header="{TemplateBinding Content}" IsCheckable="False" IsChecked="{TemplateBinding IsChecked}" IsHitTestVisible="False"/>
</Border>
</ControlTemplate>
</Page.Resources>
<StackPanel>
<RadioButton Content="Foo" IsChecked="True" Template="{StaticResource Template}"/>
<RadioButton Content="Bar" Template="{StaticResource Template}"/>
<RadioButton Content="Biz" Template="{StaticResource Template}"/>
</StackPanel>
</Page>
不隶属于 StackOverflow