Wpf 是否可以使用可检查菜单项作为RadioButton的控制模板?
我在我的WPF应用程序中有一个菜单,有多个选项,就像一个单选按钮组(选择一个可以取消选择其余的选项)。我想使用可检查的菜单项作为单选按钮的模板 我试图设置模板,但它似乎不像人们期望的那样工作。选择和取消选择项目似乎与单选按钮的值不同步 我想我可以使用更复杂的模板,并使用路径或其他方式“伪造”所选标记,但对于这样一个简单的目的来说,似乎要做很多工作。另外,当使用更复杂的模板时,我必须处理不同的主题,而我不想这样做 这里有一个简单的例子来说明这个问题Wpf 是否可以使用可检查菜单项作为RadioButton的控制模板?,wpf,radio-button,controltemplate,Wpf,Radio Button,Controltemplate,我在我的WPF应用程序中有一个菜单,有多个选项,就像一个单选按钮组(选择一个可以取消选择其余的选项)。我想使用可检查的菜单项作为单选按钮的模板 我试图设置模板,但它似乎不像人们期望的那样工作。选择和取消选择项目似乎与单选按钮的值不同步 我想我可以使用更复杂的模板,并使用路径或其他方式“伪造”所选标记,但对于这样一个简单的目的来说,似乎要做很多工作。另外,当使用更复杂的模板时,我必须处理不同的主题,而我不想这样做 这里有一个简单的例子来说明这个问题 <Page xmlns="http://s
<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
上的ishitestvisible
设置为false并添加一个边框来吸收鼠标事件时,它似乎像您期望的那样工作:
<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>