C# 访问后更改项目的背景色
我有以下代码:C# 访问后更改项目的背景色,c#,wpf,xaml,C#,Wpf,Xaml,我有以下代码: <ItemsControl Grid.Row="1" ItemsSource="{Binding Activities}"> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <ctrls:AlignableWrapPan
<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>
<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>
现在,我希望更改已单击的每个项目的背景色(这将导致一个关于该项目的页面)。再次单击该项目时,背景颜色应重新更改
我该怎么做
我已经看过了。有一个答案包含以下代码:
<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>
<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设置样式。此外,它可能并不完全符合我的要求。您可以组合这些样式。但是有一个陷阱:在Windows8+中重写
SystemColors.HighlighBrush
对您没有帮助。另外,您需要启用multiselect,它不适用于ItemsControl
,但ListBox
有它。请尝试以下代码:
<ListBox ItemsSource="{Binding Activities}" SelectionMode="Multiple">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<ctrls:AlignableWrapPanel MaxWidth="400" HorizontalContentAlignment="Center" HorizontalAlignment="Center" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border>
<ContentPresenter Content="{Binding}"/>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalAlignment" Value="Center" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border Background="{TemplateBinding Background}">
<ContentPresenter Content="{TemplateBinding Content}"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsSelected" Value="True" >
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="Background" Value="Yellow" />
<Setter Property="Foreground" Value="Black" />
</Trigger>
</Style.Triggers>
</Style>
</ItemsControl.ItemContainerStyle>
</ListBox>
该代码起到了关键作用:
<ItemsControl ...>
...
<ItemsControl.Resources>
<Style TargetType="ToggleButton">
<Style.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Background" Value="Green" />
</Trigger>
</Style.Triggers>
</Style>
</ItemsControl.Resources>
<ItemsControl.ItemTemplate>
<DataTemplate>
<ToggleButton Content="{Binding}">
<ToggleButton.Template>
<ControlTemplate>
<Label Content="{Binding}" Background="{TemplateBinding Background}" />
</ControlTemplate>
</ToggleButton.Template>
</ToggleButton>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
...
您可以将类似“IsVisited”的属性添加到项目的viewmodel(“活动”的项目类型)
每次单击项目时,将此属性设置为true/false,例如,使用与打开详细信息页面相同的方法
然后,在项目的DataTemplate中,您可以创建绑定到“IsVisited”属性的Datatrigger,并设置项目面板的背景色
请提供更多代码以获得更详细的帮助