【发布时间】:2016-04-19 08:11:20
【问题描述】:
我有以下代码:
<ItemsControl Grid.Row="1" ItemsSource="{Binding Activities}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<ctrls:AlignableWrapPanel MaxWidth="400" HorizontalContentAlignment="Center"
HorizontalAlignment="Center" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="HorizontalAlignment" Value="Center" />
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>
现在我想更改已点击的每个项目的背景颜色(这会导致关于该项目的页面)。再次单击该项目时,背景颜色应变回。
我该怎么做?
我已经看过Change background color for selected ListBox item。有一个答案包含以下代码:
<Style TargetType="ListBoxItem">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True" >
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="Background" Value="Transparent" />
<Setter Property="Foreground" Value="Black" />
</Trigger>
</Style.Triggers>
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/>
</Style.Resources>
</Style>
唉,因为我已经在设置 ContentPresenter 的样式,所以我不能轻易地在我的代码中插入此代码。此外,它可能并不完全符合我的要求。
【问题讨论】: