wpf 列表框复选框在选中或选择时更改颜色
我尝试过此网站上的不同示例,以使列表框/复选框组合在选择时从默认灰色更改为另一种颜色,但无济于事。
我最终想做的是,如果选中该项目,背景将为白色,未选中时背景将为灰色。
这是我所拥有的,任何帮助将不胜感激。
将资源更新到下面的评论。
控件已更新至回复,但仍然无法正常工作,有什么想法吗?
<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¤t=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.
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.
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.
Any ideas?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的示例没有使用
myListboxStyle
样式,您可以将其删除。但是更改ItemContainerStyle
属性:这是一个非常简单的模板,只有两个触发器:
IsSelected=True
和IsSelected=False
。要完成此示例,请将这些画笔添加到
Resources
集合中:最好编辑
ListViewItem
的标准样式,但我在互联网上找不到它我现在没有 Expression Blend。结果屏幕:
My example doesn't use the
myListboxStyle
style, you can remove it. But change theItemContainerStyle
property:It is very simple template and it has only two triggers:
IsSelected=True
andIsSelected=False
.And to complete this example add these brushes to the
Resources
collection: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: