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 ?
答案 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>