Radiobutton의 컨트롤 템플릿으로 Checkable Menuitem을 사용할 수 있습니까?
-
19-08-2019 - |
문제
라디오 버튼 그룹처럼 작동하는 여러 옵션이있는 WPF 애플리케이션에 메뉴가 있습니다 (나머지 중 선택 사항 선택). Checkable 메뉴 항목을 라디오 버튼의 템플릿으로 사용하고 싶습니다.
템플릿을 설정하려고했지만 예상 한 것처럼 작동하지 않는 것 같습니다. 항목을 선택하고 선택 해제하는 것은 라디오 버튼의 값과 동기화되지 않는 것 같습니다.
나는 더 복잡한 템플릿을 사용할 수 있고 경로 나 무언가를 사용하여 선택한 마크를 "가짜"할 수 있다고 생각하지만 간단한 목적을 위해 많은 작업이 보입니다. 또한 더 복잡한 템플릿을 사용할 때는 원하지 않는 다른 테마를 다루어야합니다.
다음은 문제를 보여주는 간단한 예입니다.
<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
에게 거짓 그리고 추가 a 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