如何更改单个 Listview 的项目 ItemTemplate?
How can i change single Listview's item ItemTemplate?
XAML:
<DataTemplate x:key="AwesomeDataTemplate" >
(Awesome stuff here...)
</DataTemplate>
<DataTemplate x:key="GodLikeDataTemplate" >
(Something better here...)
</DataTemplate>
我有一个 Listview
,里面有一些 ListViewItem
。 ListView
的 ItemTemplate
默认是 AwesomeDataTemplate。当我悬停在 ListViewItem
上时,是否可以将悬停的 ListViewItem
的项目模板更改为 GodLikeDataTemplate?
例如,您可以做类似的事情。使用触发器并更改 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>
XAML:
<DataTemplate x:key="AwesomeDataTemplate" >
(Awesome stuff here...)
</DataTemplate>
<DataTemplate x:key="GodLikeDataTemplate" >
(Something better here...)
</DataTemplate>
我有一个 Listview
,里面有一些 ListViewItem
。 ListView
的 ItemTemplate
默认是 AwesomeDataTemplate。当我悬停在 ListViewItem
上时,是否可以将悬停的 ListViewItem
的项目模板更改为 GodLikeDataTemplate?
例如,您可以做类似的事情。使用触发器并更改 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>