为什么我定制的WPF单选按钮看起来不正确?
我正在将单选按钮的样式替换为复选框的样式。我知道你会说这是个坏主意,但它确实需要这样做。无论如何,使用Expression Blend,我能够提取复选框的样式并将其应用于RadioButton。我唯一的问题是,当它绘制时,没有边界。谁能告诉我为什么?以下是代码(自最初发布后已更新):为什么我定制的WPF单选按钮看起来不正确?,wpf,checkbox,radio-button,Wpf,Checkbox,Radio Button,我正在将单选按钮的样式替换为复选框的样式。我知道你会说这是个坏主意,但它确实需要这样做。无论如何,使用Expression Blend,我能够提取复选框的样式并将其应用于RadioButton。我唯一的问题是,当它绘制时,没有边界。谁能告诉我为什么?以下是代码(自最初发布后已更新): 复选框 单选按钮 若要查看如何显示,请将其粘贴到Visual Studio中。您将看到定制的单选按钮看起来不正确。它缺少复选框通常具有的常规3D效果。我不关心任何内容(我不会有任何内容),我只希望它看起来像
复选框
单选按钮
若要查看如何显示,请将其粘贴到Visual Studio中。您将看到定制的单选按钮看起来不正确。它缺少复选框通常具有的常规3D效果。我不关心任何内容(我不会有任何内容),我只希望它看起来像一个普通的复选框。因为您要覆盖模板,所以您需要自己附加到类的属性 以以下方式在模板中声明边框:
<Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"/>
还有,你有一个小虫子。您已经在样式中多次定义了“BorderBrush”的setter
编辑:看到您的图像后,这里是您真正的问题:
<Microsoft_Windows_Themes:BulletChrome Background="{TemplateBinding Background}" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" IsChecked="{TemplateBinding IsChecked}" IsRound="false" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderPressed="{TemplateBinding IsPressed}"/>
你的子弹没有击中边缘。它有效地将您的厚度设置为0,这就是您没有看到任何东西的原因。在这里,图像可能会有所帮助,但您希望边框在哪里?您是否希望整个内容周围有边框??根据您使用的模板,唯一的边框应该是复选标记所在的周围。如果要在所有内容周围添加边框,则需要在
控制模板中添加边框
,如下所示:
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type RadioButton}">
<Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
<BulletDecorator Background="Transparent">
<BulletDecorator.Bullet>
<Microsoft_Windows_Themes:BulletChrome Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" IsChecked="{TemplateBinding IsChecked}" IsRound="false" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderPressed="{TemplateBinding IsPressed}"/>
</BulletDecorator.Bullet>
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignme nt}" RecognizesAccessKey="True"/>
</BulletDecorator>
<ControlTemplate.Triggers>
<Trigger Property="HasContent" Value="true">
<Setter Property="FocusVisualStyle" Value="{StaticResource CheckRadioFocusVisual}"/>
<Setter Property="Padding" Value="4,0,0,0"/>
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
旁注:您已经在复选框本身上设置了FocusVisualStyle=“{x:Null}”
,但是您的模板在其HasContent
属性触发器中更改了此属性。如果您真的不想要FocusVisualStyle,您应该删除该setter。我应该在哪里添加该代码?我试着放入ControlTemplate,但它无法编译。谢谢你的帮助,威尔。这取决于你希望边界包围什么。正如Drew Marsh的回答所指出的那样,我假设在你的RadioButtonCheckBoxStyle
中声明的ControlTemplate中,但你可以在你的
中尝试。请看我的编辑,你的bullet声明中缺少BorderThickness
。Will Eddins的修订答案是正确的,您缺少BulletChrome上的BorderThickness。如果您想要复选框的完整外观,只需使用复选框,并在选择其他复选框时手动取消选中组中的其他复选框可能会更容易。虽然我下面的答案可能会让你的边界恢复,但它不一定会产生完整的3D效果。看看:
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type RadioButton}">
<Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
<BulletDecorator Background="Transparent">
<BulletDecorator.Bullet>
<Microsoft_Windows_Themes:BulletChrome Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" IsChecked="{TemplateBinding IsChecked}" IsRound="false" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderPressed="{TemplateBinding IsPressed}"/>
</BulletDecorator.Bullet>
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignme nt}" RecognizesAccessKey="True"/>
</BulletDecorator>
<ControlTemplate.Triggers>
<Trigger Property="HasContent" Value="true">
<Setter Property="FocusVisualStyle" Value="{StaticResource CheckRadioFocusVisual}"/>
<Setter Property="Padding" Value="4,0,0,0"/>
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>