[英]ListView different template when selected
我正在尝试为列表视图项选择一个不同的模板,但是我找不到有关此操作的任何信息。
根据是否选择了列表视图项,选择模板的最佳方法是什么?
根据是否选择了列表视图项,选择模板的最佳方法是什么?
正确的方法是更改默认模板并在VisualState
自定义动画。
在项目中复制并粘贴默认的ListViewItem样式和模板,请参见此处
在Selected
和PointerOverSelected
视觉状态下更改画笔:
处于Selected
可视状态的SystemControlHighlightListAccentLowBrush
:
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="BorderBackground" Storyboard.TargetProperty="Fill">
<DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightListAccentLowBrush}" />
</ObjectAnimationUsingKeyFrames>
改成:
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="BorderBackground" Storyboard.TargetProperty="Fill">
<DiscreteObjectKeyFrame KeyTime="0" Value="Red" />
</ObjectAnimationUsingKeyFrames>
PointerOverSelected
可视状态下的SystemControlHighlightListAccentMediumBrush
:
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="BorderBackground" Storyboard.TargetProperty="Fill">
<DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightListAccentMediumBrush}" />
</ObjectAnimationUsingKeyFrames>
改成:
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="BorderBackground" Storyboard.TargetProperty="Fill">
<DiscreteObjectKeyFrame KeyTime="0" Value="Red" />
</ObjectAnimationUsingKeyFrames>
在这里检查我完成的样本
-----更新(09/27/2016)-----
选择列表框后,如何使用它来更改ListBoxItem的DataTemplate
如果需要切换DataTemplate
,则可以从后面的代码中进行更改。
1)在页面资源中附加一个DataTeemplate:
<Page.Resources>
<DataTemplate x:Key="dataTemplate1">
<StackPanel Orientation="Horizontal">
<TextBlock Text="->" />
<TextBlock Text="{Binding}" />
</StackPanel>
</DataTemplate>
</Page.Resources>
2)为SelectionChanged
事件添加处理程序:
<ListView SelectionChanged="ListView_SelectionChanged">
<ListView.Items>
<ListViewItem Content="One"></ListViewItem>
<ListViewItem Content="Two"></ListViewItem>
<ListViewItem Content="Three"></ListViewItem>
</ListView.Items>
</ListView>
3)从代码后面更改DataTemplate
private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
//Assign DataTemplate for selected items
foreach (var item in e.AddedItems)
{
ListViewItem _lvi = item as ListViewItem;
_lvi.ContentTemplate = (DataTemplate)this.Resources["dataTemplate1"];
}
//Remove DataTemplate for unselected items
foreach (var item in e.RemovedItems)
{
ListViewItem _lvi = item as ListViewItem;
_lvi.ContentTemplate = null;
}
}
更新了我的演示: LINK
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.