WPF更改ListBox背景颜色,启用和禁用

时间:2017-07-05 16:03:26

标签: c# wpf listbox

我正在搜索如何更改WPF列表框的背景颜色,没有任何效果......我不想更改项目模板的背景,而是更改ListBox本身的背景。
我尝试了这里回答的不同解决方案Answers
这是我的列表框:

<ListBox Name="myListBox" ScrollViewer.VerticalScrollBarVisibility="Visible" SelectionChanged="myListBox_SelectionChanged" Background="#FFC3DDF7" Margin="0,0,0,10">
                <ListBox.ItemContainerStyle>
                    <Style TargetType="ListBoxItem">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Value.IsOk}" Value="True">
                                <Setter Property="Background" Value="Green"/>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </ListBox.ItemContainerStyle>
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <StackPanel Width="200">
                                <TextBlock FontSize="10" FontWeight="Bold" VerticalAlignment="Center" Text="{Binding Path=Key}" />
                                <TextBlock FontSize="10" VerticalAlignment="Center" TextWrapping="Wrap">
                                    <TextBlock.Text>
                                        <MultiBinding StringFormat="{}{0} {1} {2}">
                                            <Binding Path="Value.TextA" />
                                            <Binding Path="Value.TextB" />
                                            <Binding Path="Value.TextC" />
                                        </MultiBinding>
                                    </TextBlock.Text>
                                </TextBlock>
                            </StackPanel>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

也许项目模板在前台?

试过这些代码:

<ListBox.Style>
<Style TargetType="{x:Type ListBox}">
    <Style.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Blue"/>
    </Style.Resources>
</Style>

 <ListBox>
    <ListBox.Style>
        <Style TargetType="{x:Type ListBox}">
            <Style.Triggers>
                <Trigger Property="IsEnabled" Value="False">
                    <Setter Property="Background" Value="Blue" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </ListBox.Style>
</ListBox>

不工作。两个滚动条之间的右下角变成了蓝色......但这就是全部!

3 个答案:

答案 0 :(得分:2)

您的链接答案不起作用的原因是因为您将列表框的背景设置为元素本身的#FFC3DDF7。如果您希望更改列表框的初始背景颜色,则需要将其移动到样式中。如果你不移动它,那么&#34;最接近&#34;定义的值将是元素本身的值,并且样式不能覆盖它。

<ListBox Name="myListBox" ScrollViewer.VerticalScrollBarVisibility="Visible" SelectionChanged="myListBox_SelectionChanged" Margin="0,0,0,10">
        <ListBox.Style>
            <Style TargetType="{x:Type ListBox}">
                <Setter Property="Background" Value="#FFC3DDF7"/>

                <Style.Triggers>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter Property="Foreground" Value="LightGray" />
                        <Setter Property="Background" Value="LightGray" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </ListBox.Style>

        <ListBox.ItemContainerStyle>
            <Style TargetType="ListBoxItem">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Value.IsOk}" Value="True">
                        <Setter Property="Background" Value="Green"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </ListBox.ItemContainerStyle>

        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <StackPanel Width="200">
                        <TextBlock FontSize="10" FontWeight="Bold" VerticalAlignment="Center" Text="{Binding Path=Key}" />
                        <TextBlock FontSize="10" VerticalAlignment="Center" TextWrapping="Wrap">
                            <TextBlock.Text>
                                <MultiBinding StringFormat="{}{0} {1} {2}">
                                    <Binding Path="Value.TextA" />
                                    <Binding Path="Value.TextB" />
                                    <Binding Path="Value.TextC" />
                                </MultiBinding>
                            </TextBlock.Text>
                        </TextBlock>
                    </StackPanel>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

答案 1 :(得分:1)

首先关闭所有 - 这个msdn链接ListBox Styles and Templates将对您有所帮助。您可以通过这种方式进行检查 - 适用于ListBoxListBoxItem

的内容

如果您检查Style的{​​{1}},您会看到ListBoxItem未获得ContentPresenter

Foreground

无论如何,这是你的解决方案:

  <Style TargetType="ListBoxItem">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ListBoxItem">
                        <Grid Background="{TemplateBinding Background}">
                            <ContentPresenter   x:Name="contentPresenter"
                                                Content="{TemplateBinding Content}"
                                                ContentTemplate="{TemplateBinding ContentTemplate}"
                                                HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                                Margin="{TemplateBinding Padding}"/><!-- No foreground gets applied. -->
                            <Rectangle x:Name="FocusVisualElement" Stroke="#FF6DBDD1" StrokeThickness="1" Visibility="Collapsed" RadiusX="1" RadiusY="1" />
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

<ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal" Width="200"> <TextBlock VerticalAlignment="Center" FontSize="10" TextWrapping="Wrap"> <Run FontWeight="Bold" Text="{Binding Path=Key}" /> <LineBreak /> <Run> <Run.Text> <MultiBinding StringFormat="{}{0} {1} {2}"> <Binding Path="Value.TextA" /> <Binding Path="Value.TextB" /> <Binding Path="Value.TextC" /> </MultiBinding> </Run.Text> </Run> <TextBlock.Style> <Style TargetType="{x:Type TextBlock}"> <Style.Triggers> <DataTrigger Binding="{Binding IsOk}" Value="True" > <Setter Property="Foreground" Value="Green"/> </DataTrigger> </Style.Triggers> </Style> </TextBlock.Style> </TextBlock> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> 现在收听TextBlock并将应用isOk

答案 2 :(得分:1)

正如Anthony指出的那样,你的样式触发器永远不会设置ListBox的背景,因为你设置了一个本地值。它被称为“依赖属性优先级”。请看一下这个链接:https://docs.microsoft.com/en-us/dotnet/framework/wpf/advanced/dependency-property-value-precedence

“本地”值的优先级高于“样式触发器”值。但是,“样式触发器”值优先于“样式设置器”值。这意味着如果您想拥有属性的初始值并根据触发器更改它,只需将其设置为最初的样式,而不是像代码那样在本地设置它。