wpf 列表框复选框在选中或选择时更改颜色

发布于 2024-10-29 16:16:21 字数 3624 浏览 3 评论 0原文

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

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

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

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

控件已更新至回复,但仍然无法正常工作,有什么想法吗?

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

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

随附的屏幕截图似乎与下面的回复相符。我很困惑。

screen shot

这是查看更大图像的直接链接。
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

即使设置了画笔,对于某些它们没有被触发的原因。

在此处输入图像描述

有什么想法吗?

谢谢。

I have tried the different examples on this site to get the list box / check box combo to change from the default gray when selected to another color to no avail.

What I am trying to do in the end is if the item is checked, the background will be white, and when unchecked it will gray out.

Here is what I have and any help would be appreciated.

Update the resource to the comment below.

The control has been updated to the reply and still not working, any ideas?

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

Note: The checked checkboxes and list item combination is still gray and the unchecked is white.

Attached is a screen shot which seems to match the reply have below. I am stumped.

screen shot

Here is the direct link in order to see the image bigger.
http://s1120.photobucket.com/albums/l489/nitefrog/?action=view¤t=jw_0012011-03-311325.jpg

Here is the screen shot of the checkboxes.

enter image description here

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

Even though the brushes are set, for some reason they are not being triggered.

enter image description here

Any ideas?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

秋千易 2024-11-05 16:16:21

我的示例没有使用 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。

结果屏幕:
在此处输入图像描述

My example doesn't use the myListboxStyle style, you can remove it. But change the ItemContainerStyle property:

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

It is very simple template and it has only two triggers: IsSelected=True and IsSelected=False.
And to complete this example add these brushes to the Resources collection:

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

It would be better to edit the standard style for the ListViewItem, but I can't find it in the internet and I don't have Expression Blend now.

Screen of the result:
enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文