在ListView中设置所选项目背景

时间:2014-04-23 16:52:14

标签: wpf listview wpf-controls

以下代码的工作原理是列表项目按预期显示,如果我"点击"在一个它成为"选择的项目"并且匹配lvCategory.IsSelected后面的代码是真的。但是,显示的项目背景不会改变以指示它已被选中。我究竟做错了什么?谢谢。

<Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:GCDataLayer="clr-namespace:GCDataLayer;assembly=GCDataLayer" mc:Ignorable="d"
    Title="MultiTest" Height="350" Width="525">
    <Window.Resources>
        <CollectionViewSource x:Key="CategoryViewSource" 
           d:DesignSource="{d:DesignInstance {x:Type GCDataLayer:Category}, 
                CreateList=True}"/>
        <ControlTemplate x:Key="ItemTemplate" TargetType="ListViewItem">
            <Border
        BorderThickness="{TemplateBinding Border.BorderThickness}"
        Padding="{TemplateBinding Control.Padding}"
        BorderBrush="{TemplateBinding Border.BorderBrush}"
        Background="{TemplateBinding Panel.Background}"
        SnapsToDevicePixels="True">
                <ContentPresenter
            Content="{TemplateBinding ContentControl.Content}"
            ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}"
            HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}"
            VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}"
            SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
            </Border>
        </ControlTemplate>
        <Style TargetType="ListViewItem">
            <Setter Property="Template" Value="{StaticResource ItemTemplate}" />
        </Style>          
        <DataTemplate x:Key="CategoryItemDataTemplate">          
            <TextBox
            Text="{Binding Path=CategoryName}"></TextBox>
        </DataTemplate>          
    </Window.Resources>
    <StackPanel Orientation="Vertical">
        <GroupBox DataContext="{StaticResource CategoryViewSource}" Margin="5"  Header="Category" BorderThickness="2">
            <ListView ScrollViewer.HorizontalScrollBarVisibility="Disabled"                        Name="lvCategory"         
            SelectionMode="Single"
            ItemsSource="{Binding}"
            ItemTemplate="{StaticResource CategoryItemDataTemplate}">
                <ListView.ItemsPanel>
                    <ItemsPanelTemplate>
                        <WrapPanel/>
                    </ItemsPanelTemplate>
                </ListView.ItemsPanel>
            </ListView>
        </GroupBox>
    </StackPanel>
</Window>

1 个答案:

答案 0 :(得分:0)

覆盖 ListViewItem 的默认模板。因此,删除其默认触发器和负责以不同颜色显示所选项目的Visual状态。

如果您不希望任何外观变化,请删除资源部分下的默认样式声明,即

删除这些资源,它可以正常工作:

<ControlTemplate x:Key="ItemTemplate" TargetType="ListViewItem">
        <Border
    BorderThickness="{TemplateBinding Border.BorderThickness}"
    Padding="{TemplateBinding Control.Padding}"
    BorderBrush="{TemplateBinding Border.BorderBrush}"
    Background="{TemplateBinding Panel.Background}"
    SnapsToDevicePixels="True">
   <ContentPresenter
       Content="{TemplateBinding ContentControl.Content}"
       ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}"
       HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}"
       VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}"
       SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
       </Border>
    </ControlTemplate>
    <Style TargetType="ListViewItem">
        <Setter Property="Template" Value="{StaticResource ItemTemplate}" />
    </Style>