【问题标题】:wpf listbox checkbox change color when checked or selectedwpf 列表框复选框在选中或选中时更改颜色
【发布时间】:2011-03-31 08:14:04
【问题描述】:

我尝试了此站点上的不同示例,以使列表框/复选框组合在选择时从默认灰色更改为另一种颜色,但无济于事。

我最终要做的是,如果选中该项目,则背景将为白色,未选中时它将变灰。

这是我所拥有的,如果有任何帮助,我们将不胜感激。

将资源更新为下面的评论。

控件已经更新到回复了,还是不行,有什么想法吗?

<ListBox ScrollViewer.HorizontalScrollBarVisibility="Disabled" 
            ItemsSource="{Binding}" 
            Name="lstSwimLane" SelectionMode="Multiple"
            Width="auto" 
            Height="auto"
            Background="Transparent"
            BorderThickness="0" 
            SelectionChanged="LstSwimLaneSelectionChanged">

    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel IsItemsHost="True" />
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>

    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="IsSelected" Value="{Binding Path=IsChecked, Mode=TwoWay}" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ListBoxItem">
                        <Border x:Name="Border" SnapsToDevicePixels="true">
                            <ContentPresenter />
                        </Border>

                        <ControlTemplate.Triggers>
                            <Trigger Property="IsSelected" Value="True">
                                <Setter TargetName="Border" Property="Background" Value="{StaticResource SelectedBrush}"/>
                            </Trigger>
                            <Trigger Property="IsSelected" Value="False">
                                <Setter TargetName="Border" Property="Background" Value="{StaticResource UnselectedBrush}"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ListBox.ItemContainerStyle>

    <ListBox.ItemTemplate>                                    
        <DataTemplate>
            <StackPanel Orientation="Horizontal" Margin="3,3,3,3">
                <CheckBox IsChecked="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}, Path=IsSelected}"
                                Checked="ChkFilterChecked" 
                                Unchecked="ChkFilterUnchecked" 
                                VerticalAlignment="Center" 
                                Margin="0,0,4,0" />
                <TextBlock Text="{Binding Value}" VerticalAlignment="Center" />
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

注意:选中的复选框和列表项组合仍为灰色,未选中的为白色。

附件是一个屏幕截图,似乎与下面的回复相匹配。我难住了。

这里是直接链接,以便查看更大的图像。
http://s1120.photobucket.com/albums/l489/nitefrog/?action=view&current=jw_0012011-03-311325.jpg

这是复选框的屏幕截图。

http://i1120.photobucket.com/albums/l489/nitefrog/jw_0022011-03-311345.jpg

即使设置了画笔,但由于某种原因它们没有被触发。

有什么想法吗?

谢谢。

【问题讨论】:

    标签: wpf checkbox listbox selecteditem listitem


    【解决方案1】:

    我的示例没有使用myListboxStyle 样式,您可以将其删除。但是更改ItemContainerStyle 属性:

            <ListBox.ItemContainerStyle>
                <Style TargetType="ListBoxItem">
                    <Setter Property="IsSelected" Value="{Binding Path=IsChecked, Mode=TwoWay}" />
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="ListBoxItem">
                                <Border x:Name="Border" SnapsToDevicePixels="true">
                                    <ContentPresenter />
                                </Border>
    
                                <ControlTemplate.Triggers>
                                    <Trigger Property="IsSelected" Value="True">
                                        <Setter TargetName="Border" Property="Background" Value="{StaticResource SelectedBrush}"/>
                                    </Trigger>
                                    <Trigger Property="IsSelected" Value="False">
                                        <Setter TargetName="Border" Property="Background" Value="{StaticResource UnselectedBrush}"/>
                                    </Trigger>
                                </ControlTemplate.Triggers>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </ListBox.ItemContainerStyle>
    

    这是一个非常简单的模板,它只有两个触发器:IsSelected=TrueIsSelected=False。 为了完成这个示例,请将这些画笔添加到 Resources 集合中:

        <SolidColorBrush x:Key="SelectedBrush" Color="White"/>
        <SolidColorBrush x:Key="UnselectedBrush" Color="Gray"/>
    

    编辑ListViewItem的标准样式会更好,但我在互联网上找不到它,我现在没有Expression Blend。

    结果屏幕:

    【讨论】:

    • 不起作用。选中的所有内容仍为灰色,未选中的所有内容均为白色。我剪切并粘贴了上面的代码。
    • @nitefrog 将您当前的代码与上面的屏幕截图进行比较。在我的应用程序中,选中状态为白色,未选中为灰色,一切随你所需。
    • @nitefrog 这很奇怪,但是您问题中的代码具有相同的结果:未选中的灰色和选中的白色。可能问题出在Resources 元素中的两个画笔上。
    • @vortex 我刚刚上传了两个屏幕截图给你看。我对这个完全不知所措。
    • @nitefrog 刷机没有触发,大概我知道问题出在哪里。尝试删除带有事件处理程序SelectionChanged="LstSwimLaneSelectionChanged" 的行。在我看来,您在代码隐藏的某处更改了ListBoxItemIsSelected 属性。
    猜你喜欢
    • 2021-05-24
    • 1970-01-01
    • 1970-01-01
    • 2020-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多