所选项目背景? [英] Selected Item Background?
问题描述
我有一个带按钮DataTemplate
的ListBox
. ListBox
视图项在ToggleButton
列表中.
I have a ListBox
with a buttons DataTemplate
. The ListBox
view items are in a list of ToggleButton
s.
我的问题是选定的项目背景如图所示:
My problem is the with selected item background as shown in picture:
如图所示,问题出在按钮一上.此屏幕快照是在首次加载wpf页面时(在选中切换按钮之前)拍摄的.如果然后我检查切换按钮(按钮一),边框中的白色将消失
As shown, the problem is in button one. This screenshot was taken when the wpf page is first loaded (before the toggle button is checked). If I then check the togglebutton (button one) the white in the border will disappear
我不希望显示白色(如两个按钮). 我有:
I don't want this white color shown (like two button). I have:
<ListBox x:Name="lbname" ItemsSource="{Binding myCollection}" Margin="432,110,0,158" Width="214" HorizontalAlignment="Left" Background="{x:Null}" BorderBrush="{x:Null}">
<ListBox.Resources>
<Style TargetType="ListBoxItem">
<Style.Resources>
<!-- SelectedItem with focus -->
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
Color="Transparent" />
<!-- SelectedItem without focus -->
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}"
Color="Transparent" />
<!-- SelectedItem text foreground -->
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}"
Color="Black" />
</Style.Resources>
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
</Style>
</ListBox.Resources>
<ListBox.ItemTemplate>
<DataTemplate>
<ToggleButton x:Name="btnname" tyle="{StaticResource ToggleStyle}" FontFamily="tahoma" FontSize="14" Height="55" Width="208" FontWeight="Normal" IsChecked="{Binding IsSelected, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}}" Background="{x:Null}" BorderBrush="{x:Null}" Checked="btnname_Checked" >
<Grid>
<Image Height="55" Width="205">
<Image.Style>
<Style>
<Setter Property="Image.Source" Value="/myApplication;component/images/buttons/two.png" />
<Style.Triggers>
<DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}}" Value="True">
<Setter Property="Image.Source" Value="/myApplication;component/images/buttons/one.png" />
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
<TextBlock Text="{Binding Name}" HorizontalAlignment="Center" VerticalAlignment="Center" />
</Grid>
</ToggleButton>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
关于如何解决此问题的任何建议?
Any suggestions on how to fix this?
推荐答案
您需要添加此Style
以覆盖ListBoxItem
的默认样式:
You need to add this Style
to override the default styling of the ListBoxItem
:
<Style x:Key="HiddenDefaultSelectionStyle" TargetType="{x:Type ListBoxItem}">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black" />
<SolidColorBrush x:Key="{x:Static SystemColors.ControlTextBrushKey}" Color="Black" />
</Style.Resources>
</Style>
您当然可以用自己喜欢的颜色替换这些颜色.您可以像这样使用它:
You can of course replace these colours with whatever you prefer. You can use it like this:
<ListBox ItemContainerStyle="{StaticResource HiddenDefaultSelectionStyle}" />
或者您可以删除x:Key="HiddenDefaultSelectionStyle"
声明,然后它将影响作用域中的每个ListBoxItem
.
Or you can remove the x:Key="HiddenDefaultSelectionStyle"
declaration and then it will affect every ListBoxItem
in scope.
这篇关于所选项目背景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!