C# WPF 复选框样式更改
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13684355/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF Checkbox style change
提问by xandi1987
I have just started with WPF and need specific feature for a checkbox:
我刚刚开始使用 WPF,需要一个复选框的特定功能:
- I want to change the shape from a box to a ellipse.
- Futher more there should be a color change (green = true, red = false) instead of cross.
- 我想将形状从盒子更改为椭圆。
- 此外,应该有颜色变化(绿色 = 真,红色 = 假)而不是交叉。
The background: I have different sensors and want to enable/disable them via these checkboxes. I also thought about to use buttons instead of checkboxes, but I think the function is more given by the checkboxes.
背景:我有不同的传感器,想通过这些复选框启用/禁用它们。我也考虑过使用按钮而不是复选框,但我认为功能更多是由复选框提供的。
I hope my description is understandable. Is it possible to define such a style- template?
我希望我的描述是可以理解的。是否可以定义这样的样式模板?
Kind regards
亲切的问候
Alex
亚历克斯
采纳答案by Blachshma
You're going to need to customize the Checkbox and create a Custom Template.
For that you need to change the default Checkbox Template.
您将需要自定义 Checkbox 并创建一个Custom Template。
为此,您需要更改默认的 Checkbox Template。
What you want to do is play a little bit with the triggers and background to style it like you want. Notice the CheckMarkproperty, you'll probably want to keep it Hidden
您想要做的是稍微使用触发器和背景来设置您想要的样式。注意这个CheckMark属性,你可能想要保留它Hidden
回答by xandi1987
Ok finally I did it! In attach you can see my solution and I am confortable with the result. If someone have any remarks, please don't hesitate to write a comment. Thank you very much for your help
好吧,我终于做到了!在附件中,您可以看到我的解决方案,我对结果感到满意。如果有人有任何意见,请不要犹豫,写评论。非常感谢您的帮助
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="CheckBox" x:Key="CircleCheckbox">
<Setter Property="Cursor" Value="Hand"></Setter>
<Setter Property="Content" Value=""></Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type CheckBox}">
<Grid>
<Ellipse x:Name="outerEllipse">
<Ellipse.Fill>
<RadialGradientBrush>
<GradientStop Offset="0" Color="Red"/>
<GradientStop Offset="0.88" Color="LightCoral"/>
<GradientStop Offset="1" Color="DarkRed"/>
</RadialGradientBrush>
</Ellipse.Fill>
</Ellipse>
<Ellipse Margin="10" x:Name="highlightCircle" >
<Ellipse.Fill >
<LinearGradientBrush >
<GradientStop Offset="0" Color="Green"/>
<GradientStop Offset="0.5" Color="LightGreen"/>
<GradientStop Offset="1" Color="DarkGreen"/>
</LinearGradientBrush>
</Ellipse.Fill>
</Ellipse>
<ContentPresenter x:Name="content" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter TargetName="highlightCircle" Property="Fill">
<Setter.Value>
<LinearGradientBrush StartPoint="0.3,0" EndPoint="0.7,1">
<GradientStop Offset="0" Color="Green"/>
<GradientStop Offset="0.5" Color="LightGreen"/>
<GradientStop Offset="1" Color="DarkGreen"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
<Setter TargetName="outerEllipse" Property="Fill">
<Setter.Value>
<RadialGradientBrush>
<GradientStop Offset="0" Color="Green"/>
<GradientStop Offset="0.88" Color="LightGreen"/>
<GradientStop Offset="1" Color="DarkGreen"/>
</RadialGradientBrush>
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="IsChecked" Value="False">
<Setter TargetName="highlightCircle" Property="Fill">
<Setter.Value>
<LinearGradientBrush StartPoint="0.3,0" EndPoint="0.7,1">
<GradientStop Offset="0" Color="Red"/>
<GradientStop Offset="0.5" Color="LightCoral"/>
<GradientStop Offset="1" Color="DarkRed"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>

