如何更改单个Listview的项目ItemTemplate?

时间:2015-09-21 14:13:02

标签: c# wpf listview datatemplate listviewitem

XAML:

<DataTemplate x:key="AwesomeDataTemplate" >
  (Awesome stuff here...)
</DataTemplate>

<DataTemplate x:key="GodLikeDataTemplate" >
  (Something better here...)
</DataTemplate>

我有一个Listview,里面有一些ListViewItem个。默认情况下,ListView的{​​{1}} AwesomeDataTemplate 。当我悬停在ItemTemplate时,是否可以将悬停的ListViewItem项目模板更改为 GodLikeDataTemplate

1 个答案:

答案 0 :(得分:2)

例如,您可以做类似的事情。使用触发器并更改ContentTemplate属性的DataTemplate。

<Window x:Class="ListViewSample.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:local="clr-namespace:ListViewSample"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <Style TargetType="ListViewItem">
        <Setter Property="ContentTemplate" >
            <Setter.Value>
                <DataTemplate>
                    <TextBlock Text="{Binding}" Foreground="Blue"/>
                </DataTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="ContentTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            <TextBlock Text="{Binding}" Foreground="Red"/>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
            </Trigger>
        </Style.Triggers>
    </Style>
</Window.Resources>
<ListView>
    <ListViewItem>123</ListViewItem>
</ListView>