访问后更改项目的背景颜色

时间:2016-04-19 08:11:21

标签: c# wpf xaml

我有以下代码:

                <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设置了样式。此外,它可能并不完全符合我的要求。

3 个答案:

答案 0 :(得分:0)

您可以组合这些样式。但是有一个问题:覆盖SystemColors.HighlighBrush在Windows 8+中无法帮助您。另外,您需要启用多选,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>

答案 1 :(得分:0)

该代码可以解决问题:

<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>

答案 2 :(得分:0)

您可以将类似“IsVisited”的属性添加到项目的视图模型中(项目类型为“活动”)

每当您点击该项目时,请将此属性设置为true / false,例如在打开详细信息页面的同一方法中。

然后,在项目的DataTemplate中,您可以创建一个绑定到“IsVisited”属性的Datatrigger,并设置项目面板的背景颜色。

请提供更多代码以获取更详细的帮助。