我试图更改列表框中所选项目的背景颜色 . 之前我通过使用它做到了
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Yellow" />
哪个有效 . 但是如果我在ListBox上将IsEnabled设置为false,则ListBox的整个背景将变为ControlBrush的指定颜色 . 如果选择了ListBoxItem并且ListBox没有焦点,我只想更改颜色 .
我尝试了一些带触发器的品种,但我无法让它发挥作用 . 即使是包含IsSelected和IsFocused条件的多重触发器也不适用于我 .
有没有人有我的解决方案?
Edit: 用ItemContainerStyle尝试了我在项目中获得NullReferenceException的示例 . 它在新的解决方案中有效 . 那个's the code where it doesn'工作:
<ItemsControl ItemsSource="{Binding Path=Classification.Values}" ScrollViewer.HorizontalScrollBarVisibility="Disabled"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch" IsEnabled="{Binding Path=ClassificationEnabled}"
VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch" Grid.Row="0" x:Name="measureClassificationControl">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid Margin="2">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock Text="{Binding Category}"/>
<ListBox ItemsSource="{Binding Values.SortedList}" SelectionMode="Extended" Grid.Row="1" AlternationCount="2"
SelectionChanged="ListBox_SelectionChanged" ScrollViewer.CanContentScroll="True" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Disabled" Height="120">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock TextWrapping="NoWrap" Text="{Binding Key}">
<TextBlock.ToolTip>
<ToolTip>
<TextBlock TextWrapping="NoWrap" Text="{Binding Value}"/>
</ToolTip>
</TextBlock.ToolTip>
</TextBlock>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="2"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
3 回答
将
SystemColors.ControlBrushKey
添加到ItemContainerStyle
中 . 这样它只会影响所选项目 .Edit: 这是一个完整的Xaml示例 .
你不能同时拥有
IsSelected
True
和IsEnabled
False
. 另外IsFocused
True
和IsEnabled
False
.您的问题将
IsSelected
的背景颜色更改称为True
和IsFocused
为False .在下面的代码中(只是在你的窗口代码中粘贴
ListBox
XAML)...实现了所有这些状态组合...要检查某些东西是否已被聚焦但未被选中,你必须选择一些行然后使用"Control + Up / Down Arrow keys"来松散焦点从选定的行中聚焦其他未选中的行 .您还将观察到所选行背景的
Orange
颜色不会出现(虽然Foreground
颜色Cyan
也有效) . 对于该背景颜色更改,您必须覆盖ListBoxItem的template .我希望这能指导你正确的方向 .
现在解决了这个问题 . 问题似乎是当系统颜色被多次覆盖时XamlParser不喜欢它 . 所以我这样定义:
谢谢你的帮助!