RadioButtonのコントロールテンプレートとしてチェック可能なMenuItemを使用することはできますか?
-
19-08-2019 - |
質問
WPFアプリケーションには、ラジオボタングループのように機能する複数のオプションを持つメニューがあります(1つを選択すると、残りの選択が解除されます)。チェック可能なメニュー項目をラジオボタンのテンプレートとして使用したい。
テンプレートを設定しようとしましたが、期待どおりに動作しないようです。アイテムの選択および選択解除は、ラジオボタンの値と同期していないようです。
より複雑なテンプレートと<!> quot; fake <!> quot;を使用できると思います。パスまたは何かを使用して選択されたマークが、そのような単純な目的のために非常に多くの作業のようです。また、より複雑なテンプレートを使用する場合は、やりたくないさまざまなテーマに対処する必要があります。
問題を示す簡単な例です。
<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
のBorder
を 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>
所属していません StackOverflow