WPF使用ItemTemplate更改Listboxitem中复选标记的颜色

时间:2013-09-12 04:23:06

标签: wpf listboxitem checkedlistbox

任何WPF专家都可以告诉我如何修改我的样式,以便更改ListBoxItem中包含的Checkbox复选标记的颜色。到目前为止,我有以下从MahApps造型拼凑而成:

<Style TargetType="ListBox" BasedOn="{StaticResource MetroListBox}" x:Key="CheckBoxList" >
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListBox}">
                <Border BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}"
                            Background="{TemplateBinding Background}"
                            CornerRadius="3">
                    <ScrollViewer>
                        <ItemsPresenter />
                    </ScrollViewer>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="ItemTemplate">
        <Setter.Value>
            <DataTemplate>
                <CheckBox   IsChecked="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}, Path=IsSelected}" 
                            Content="{Binding}"/>
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>

<Style TargetType="ListBoxItem" BasedOn="{StaticResource MetroListBoxItem}">
    <Setter Property="MinHeight" Value="20" />
    <Setter Property="Margin" Value="0,0,0,5" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListBoxItem}">
                <Border x:Name="Border"
                        Background="{TemplateBinding Background}"
                        SnapsToDevicePixels="True">
                    <ContentPresenter Margin="5, 0, 0, 0" 
                                      VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter TargetName="Border" Property="Background" Value="{DynamicResource AccentSelectedColorBrush}" />
                        <Setter Property="Foreground" Value="{DynamicResource TextBrush}" />
                    </Trigger>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter TargetName="Border" Property="Background">
                            <Setter.Value>
                                <SolidColorBrush Color="{DynamicResource AccentColor3}" />
                            </Setter.Value>
                        </Setter>
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter Property="Foreground" Value="{DynamicResource GrayBrush5}" />
                    </Trigger>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="IsEnabled" Value="False" />
                            <Condition Property="IsSelected" Value="True" />
                        </MultiTrigger.Conditions>
                        <MultiTrigger.Setters>
                            <Setter TargetName="Border" Property="Background" Value="{DynamicResource GrayBrush5}" />
                            <Setter Property="Foreground" Value="{DynamicResource AccentSelectedColorBrush}" />
                        </MultiTrigger.Setters>
                    </MultiTrigger>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="IsSelected" Value="True" />
                            <Condition Property="Selector.IsSelectionActive" Value="True" />
                        </MultiTrigger.Conditions>
                        <Setter TargetName="Border" Property="Background" Value="{DynamicResource AccentSelectedColorBrush}" />
                    </MultiTrigger>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="IsMouseOver" Value="True" />
                            <Condition Property="Selector.IsSelectionActive" Value="True" />
                        </MultiTrigger.Conditions>
                        <MultiTrigger.Setters>
                            <Setter TargetName="Border" Property="Background">
                                <Setter.Value>
                                    <SolidColorBrush Color="{DynamicResource AccentColor3}" />
                                </Setter.Value>
                            </Setter>
                        </MultiTrigger.Setters>
                    </MultiTrigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

此致 艾伦

1 个答案:

答案 0 :(得分:0)

在这里,你可以参考这个问题的答案......即为复选框定义自己的模板: How to change the color of the CheckMark in WPF CheckBox control?