如何通过在 WPF 中绑定来设置复选框内容的背景颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20478518/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 10:11:05  来源:igfitidea点击:

How to set the background color of a checkbox's content by binding in WPF

wpfcheckboxstyling

提问by donL

I am trying to figure out how to bind the background color of the content for a checkbox. Here is the code I have, of course the background setting just changes the color of the checkbox not the color behind the text.

我想弄清楚如何为复选框绑定内容的背景颜色。这是我的代码,当然背景设置只会改变复选框的颜色,而不是文本背后的颜色。

        <ListBox Name="ListBox1" ItemsSource="{Binding Path=relationshipTypesTable.dataTable.DefaultView}">
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal"/>
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>

            <ListBox.ItemTemplate>
                <DataTemplate>
                    <CheckBox IsChecked="True" Content="{Binding typeDesc}" Background="{Binding color}"/>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

回答by ryrich

Try this:

尝试这个:

<ListBox Name="ListBox1" ItemsSource="{Binding Path=relationshipTypesTable.dataTable.DefaultView}">
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal"/>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>

    <ListBox.ItemTemplate>
        <DataTemplate>
            <CheckBox IsChecked="True">
                <TextBlock Text="{Binding typeDesc}" Background="{Binding color}"/>
            </CheckBox>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

回答by Chris

I don't think you're able to style the background of a CheckBoxwithout using your own ControlTemplateinstead of the default one.

我认为如果CheckBox不使用您自己的ControlTemplate而不是默认的背景,您就无法设置背景的样式。

Tou could use a ControlTemplatealong these lines (I've just grabbed the basic template from Kaxaml) - I've made the background Red- you'll want to replace that with your {Binding color}

Tou 可以使用ControlTemplate这些方法(我刚刚从 Kaxaml 中获取了基本模板) - 我已经制作了背景Red- 你会想用你的{Binding color}

 <Style x:Key="{x:Type CheckBox}" TargetType="{x:Type CheckBox}">
            <Setter Property="SnapsToDevicePixels" Value="true"/>
            <Setter Property="OverridesDefaultStyle" Value="true"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type CheckBox}">
                        <BulletDecorator Background="Transparent">
                            <BulletDecorator.Bullet>
                                <Border x:Name="Border"  
          Width="13" 
          Height="13" 
          CornerRadius="0" 
          Background="Red"
          BorderThickness="1"
          BorderBrush="#404040">
                                    <Path 
            Width="7" Height="7" 
            x:Name="CheckMark"
            SnapsToDevicePixels="False" 
            Stroke="#404040"
            StrokeThickness="2"
            Data="M 0 0 L 7 7 M 0 7 L 7 0" />
                                </Border>
                            </BulletDecorator.Bullet>
                            <ContentPresenter Margin="4,0,0,0"
        VerticalAlignment="Center"
        HorizontalAlignment="Left"
        RecognizesAccessKey="True"/>
                        </BulletDecorator>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsChecked" Value="false">
                                <Setter TargetName="CheckMark" Property="Visibility" Value="Collapsed"/>
                            </Trigger>
                            <Trigger Property="IsChecked" Value="{x:Null}">
                                <Setter TargetName="CheckMark" Property="Data" Value="M 0 7 L 7 0" />
                            </Trigger>
                            <Trigger Property="IsMouseOver" Value="true">
                                <Setter TargetName="Border" Property="Background" Value="#808080" />
                            </Trigger>
                            <Trigger Property="IsPressed" Value="true">
                                <Setter TargetName="Border" Property="Background" Value="#E0E0E0" />
                                <Setter TargetName="Border" Property="BorderBrush" Value="#606060" />
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="false">
                                <Setter TargetName="Border" Property="Background" Value="#EEEEEE" />
                                <Setter TargetName="Border" Property="BorderBrush" Value="#AAAAAA" />
                                <Setter Property="Foreground" Value="#888888"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

Which will produce something that looks like this: enter image description here

这将产生如下所示的内容: 在此处输入图片说明

You can use this style simply by placing it within a local ResourceDictionarye.g:

您只需将其放置在本地即可使用此样式,ResourceDictionary例如:

<Window....

    <Window.Resources>
        <!-- Style Goes Here -->
    </Window.Resources>

    <!-- Your Code -->
</Window>

Although it may be better off in a separate ResourceDictionary, if you intend to use throughout your application. This style will be applied to all CheckBoxes; if you wish to apply it to only a select few, you should change the x:Key="{x:Type CheckBox}"to x:Key="NameOfYourChoice", and reference the style on individual CheckBoxes- Style="{StaticResource ResourceKey=NameOfYourChoice}"

ResourceDictionary如果您打算在整个应用程序中使用,单独使用它可能会更好。这种风格将应用于所有CheckBoxes; 如果您只想将其应用于少数几个,您应该将 更改x:Key="{x:Type CheckBox}"x:Key="NameOfYourChoice",并在个人上引用样式CheckBoxes-Style="{StaticResource ResourceKey=NameOfYourChoice}"

You can style the rest to your liking. You can actually grab the default ControlTemplatefrom MSDNif you'd prefer to use that as a basis.

您可以根据自己的喜好设计其余的样式。如果您更愿意使用默认值作为基础,您实际上可以ControlTemplateMSDN获取默认值。