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

4

我正在尝试找出如何绑定复选框内容的背景色。这是我的代码,当然,背景设置只会改变复选框的颜色而不是文本背后的颜色。

        <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>

我使用了以下文章中展示的“复选框”样式。如果没有它,在我们的一个Windows Server 2008机器上,我会看到白色勾号在白色背景上:http://www.eidias.com/blog/2012/6/1/custom-wpf-check-box-with-inner-shadow-effect#cm-136 - Mike Gledhill
2个回答

6

试试这个:

<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>

谢谢@ryrich,这是我想要做的一个简单而干净的解决方案。 - donL

1

我认为你无法在不使用自己的ControlTemplate而是使用默认模板的情况下对CheckBox的背景进行样式设置。

您可以使用类似这样的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>

这将产生类似于以下内容的东西: 输入图像描述

您可以通过将其放置在本地ResourceDictionary中来简单地使用此样式,例如:

<Window....

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

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

尽管它可能更适合放在单独的ResourceDictionary中,如果您打算在整个应用程序中使用,这种样式将应用于所有CheckBoxes。如果您只希望将其应用于少数几个,请将x:Key = "{x:Type CheckBox}"更改为x:Key = "NameOfYourChoice",并在各个CheckBoxes上引用样式- Style = "{StaticResource ResourceKey = NameOfYourChoice}"
您可以根据自己的喜好对其余部分进行样式设置。实际上,如果您愿意以此为基础,可以从MSDN获取默认的ControlTemplate

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接