简体   繁体   English

如何在XAML WPF中用PNG图像替换默认的复选框样式

[英]How can replace default checkbox style with png image in xaml wpf

How can replace default checkbox style with png image, for checked state and unchecked state. 如何为选中状态和未选中状态将默认复选框样式替换为png图像。

Here is and what I try but is not complilated with xaml: 这是我尝试但不符合xaml的内容:

<CheckBox IsChecked="{Binding AirTemperatureGridChecked}">
                    <CheckBox.Background>
                        <Image Source="https://cdn4.iconfinder.com/data/icons/cc_mono_icon_set/blacks/48x48/checkbox_unchecked.png" Width="16" Height="16" />    
                    </CheckBox.Background>                        
                </CheckBox>

I try and this code but then image go in content is not replaced. 我尝试使用此代码,但是图像中的内容不会被替换。

<CheckBox IsChecked="{Binding AirTemperatureGridChecked}">
                    <Image Source="https://cdn4.iconfinder.com/data/icons/cc_mono_icon_set/blacks/48x48/checkbox_unchecked.png" Width="16" Height="16" />
                </CheckBox>

Is possible to replace default style? 是否可以替换默认样式?

In this particular case, you will need to fiddle with the Template of the CheckBox. 在这种情况下,您将需要摆弄CheckBox的模板。

<CheckBox IsChecked="{Binding AirTemperatureGridChecked}">
     <CheckBox.Template>
          <ControlTemplate TargetType="{x:Type CheckBox}">
               <Grid>
               <Image x:Name="Foo" Width="16" Height="16" Source="https://cdn4.iconfinder.com/data/icons/cc_mono_icon_set/blacks/48x48/checkbox_unchecked.png" />
               </Grid>
               <ControlTemplate.Triggers>
                    <Trigger Property="IsChecked" Value="False">
                         <Setter TargetName="Foo" Property="Opacity" Value="0.5" />
                    </Trigger>
               </ControlTemplate.Triggers>
          </ControlTemplate>
 </CheckBox.Template>

I've made this one from scratch to just give an example of how quick and easy it is to get your own templates up and running for the design of controls. 我已经从头开始制作了这个示例,仅举一个示例,说明如何快速,轻松地启动并运行自己的模板以进行控件设计。

As you can see, it's made of a grid, with your image inside, and below that is a trigger that will make the checkbox lower its opacity when unchecked. 如您所见,它是由一个网格构成的,图像位于其中,在其下方是一个触发器,当未选中时,它将使复选框降低其不透明度。

You can put just about anything in that grid to design your checkbox as you would any window, and give it functionality with triggers. 您可以像在任何窗口中一样在网格中放置任何内容来设计复选框,并为它提供带有触发器的功能。

Additionally (as giving each and every checkbox this mass of code would be unreasonable) you can give the controltemplate a key: 另外(由于为每个复选框提供了如此大量的代码,这是不合理的),您可以为controltemplate提供一个键:

<ControlTemplate x:Key="WhateverYouWantToCallMe" TargetType="{x:Type CheckBox}">
     <!-- Content ect... -->
</ControlTemplate>

Put it inside a Resource Dictionary and call it as a StaticResource for the checkbox instead, like so: 将其放在资源字典中,然后将其称为复选框的StaticResource,如下所示:

<CheckBox Template="{StaticResource WhateverYouWantToCallMe}"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM