如何解决如何在WPF复选框的可点击区域内居中?
| 如果我在WPF中创建一个CheckBox控件(不包含内容,我只需要选中/取消选中部分),它将显示\“ box \”视觉效果(具有或不具有选中项的3-D矩形)标记)在控件的左上角。 我可以将\“ box \”视觉对象放在CheckBox控件的中心吗?也就是说,水平和垂直居中?像这样: 通过将CheckBox的HorizontalAlignment和VerticalAlignment设置为Center,我可以在视觉上获得与此类似的东西。这会导致CheckBox控件缩小到其\“ box \”视觉效果的大小,然后在其父控件中居中。但是,然后它仅响应“框”视觉效果上的单击,从而显示了更小,更不方便的目标。解决方法
您可以更改复选框的模板。最简单的方法是使用StyleSnooper之类的内容复制默认模板,然后根据需要将其编辑为以下内容:
<ControlTemplate>
<BulletDecorator Background=\"Transparent\">
<aero:BulletChrome Width=\"13\" Height=\"13\" Background=\"{TemplateBinding Panel.Background}\"
BorderBrush=\"{TemplateBinding Border.BorderBrush}\" RenderMouseOver=\"{TemplateBinding UIElement.IsMouseOver}\"
RenderPressed=\"{TemplateBinding ButtonBase.IsPressed}\" IsChecked=\"{TemplateBinding ToggleButton.IsChecked}\" />
</BulletDecorator>
<ControlTemplate.Triggers>
<Trigger Property=\"ContentControl.HasContent\" Value=\"True\">
<Setter Property=\"FrameworkElement.FocusVisualStyle\">
<Setter.Value>
<Style TargetType=\"{x:Type IFrameworkInputElement}\">
<Setter Property=\"Control.Template\">
<Setter.Value>
<ControlTemplate>
<Rectangle Stroke=\"{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}\"
StrokeThickness=\"1\" StrokeDashArray=\"1 2\" Margin=\"14,0\" SnapsToDevicePixels=\"True\" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Setter.Value>
</Setter>
<Setter Property=\"Control.Padding\" Value=\"4,0\" />
</Trigger>
<Trigger Property=\"UIElement.IsEnabled\" Value=\"False\">
<Setter Property=\"TextElement.Foreground\" Value=\"{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}\" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。