【发布时间】:2011-09-16 08:52:08
【问题描述】:
我正在尝试更改列表框中所选项目的背景颜色。我之前使用过
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Yellow" />
这很有效。但是如果我在 ListBox 上将 IsEnabled 设置为 false,则 ListBox 的整个背景将变为 ControlBrush 的指定颜色。如果选择了 ListBoxItem 并且 ListBox 没有焦点,我只想更改颜色。
我尝试了一些带有触发器的品种,但我无法让它发挥作用。即使是包含 IsSelected 和 IsFocused 条件的多重触发器也不适合我。
有人可以帮我解决吗?
编辑: 使用 ItemContainerStyle 尝试了我在项目中得到 NullReferenceException 的示例。在一个新的解决方案中它可以工作。那是它不起作用的代码:
<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>
【问题讨论】:
标签: wpf listbox focus listboxitem