WPF小记 -- 使用Path自己画图标,点击命中(焦点)丢失问题
在Template中,Path外面的Grid需添加Background属性值。否则点击范围会受限制,例如:Click,在RadioButton的Height和With范围内点击,命中率<1。
<Style TargetType="{x:Type RadioButton}"> <Setter Property="BorderThickness" Value="0" /> <Setter Property="FocusVisualStyle" Value="{x:Null}" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type RadioButton}"> <!-- Grid加上Background属性,点击命中不会丢失 --> <Grid x:Name="templateRoot" Margin="5" Background="Transparent" SnapsToDevicePixels="True"> <Path Name="path" Width="20" Height="20" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Data="M100.9,100.307 L181.5,100.307 181.5,180.60001 100.9,180.60001 z M0.5,100.307 L81.099999,100.307 81.099999,180.60001 0.5,180.60001 z M100.9,0.5 L181.5,0.5 181.5,80.793 100.9,80.793 z M0.50000539,0.5 L81.100006,0.5 81.100006,80.793 0.50000539,80.793 z" Fill="#AAAAAA" Stretch="Fill" /> <ContentPresenter x:Name="contentPresenter" Margin="{TemplateBinding Padding}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Focusable="False" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" /> </Grid> <ControlTemplate.Triggers> <Trigger Property="IsChecked" Value="true"> <Setter TargetName="path" Property="Fill" Value="#5A84F3" /> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style>