Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 是否可以使用可检查菜单项作为RadioButton的控制模板?_Wpf_Radio Button_Controltemplate - Fatal编程技术网

Wpf 是否可以使用可检查菜单项作为RadioButton的控制模板?

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

我在我的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
上的
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>