ListView选择时使用不同的模板(ListView different template when selected)

编程入门 行业动态 更新时间:2024-10-26 06:39:50
ListView选择时使用不同的模板(ListView different template when selected)

我正在尝试为列表视图项目选择时创建一个不同的模板,但是我找不到任何关于这样做的信息。

根据是否选择了列表视图项,选择模板的最佳方法是什么?

I am trying to create a different template for a list view item when it is selected, however I couldn't find anything about doing this.

What is the best way to select a template based on whether the listview item is selected or not?

最满意答案

根据是否选择了列表视图项,选择模板的最佳方法是什么?

更改默认模板并在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>

截图: 在此处输入图像描述

在这里查看我完成的样本

-----更新(2016年9月27日)-----

如何在选中时使用它来更改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

What is the best way to select a template based on whether the listview item is selected or not?

Changing the default template and customize animation inside VisualState is the right way.

Copy and paste the default ListViewItem styles and templates in your project, see here

Change the brush in the Selected and PointerOverSelected visual states:

SystemControlHighlightListAccentLowBrush in the Selected visual state:

<ObjectAnimationUsingKeyFrames Storyboard.TargetName="BorderBackground" Storyboard.TargetProperty="Fill"> <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightListAccentLowBrush}" /> </ObjectAnimationUsingKeyFrames>

Change to:

<ObjectAnimationUsingKeyFrames Storyboard.TargetName="BorderBackground" Storyboard.TargetProperty="Fill"> <DiscreteObjectKeyFrame KeyTime="0" Value="Red" /> </ObjectAnimationUsingKeyFrames>

SystemControlHighlightListAccentMediumBrush in the PointerOverSelected visual state:

<ObjectAnimationUsingKeyFrames Storyboard.TargetName="BorderBackground" Storyboard.TargetProperty="Fill"> <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightListAccentMediumBrush}" /> </ObjectAnimationUsingKeyFrames>

Change to:

<ObjectAnimationUsingKeyFrames Storyboard.TargetName="BorderBackground" Storyboard.TargetProperty="Fill"> <DiscreteObjectKeyFrame KeyTime="0" Value="Red" /> </ObjectAnimationUsingKeyFrames>

Screenshot: enter image description here

Check my completed sample here

-----Update(09/27/2016)-----

How would I use this to change the DataTemplate of a ListBoxItem when it's selected

If you need to switch DataTemplate, you might change it from code behind.

1) Append a DataTeemplate in the page's resource:

<Page.Resources> <DataTemplate x:Key="dataTemplate1"> <StackPanel Orientation="Horizontal"> <TextBlock Text="->" /> <TextBlock Text="{Binding}" /> </StackPanel> </DataTemplate> </Page.Resources>

2) Add handler for SelectionChanged event:

<ListView SelectionChanged="ListView_SelectionChanged"> <ListView.Items> <ListViewItem Content="One"></ListViewItem> <ListViewItem Content="Two"></ListViewItem> <ListViewItem Content="Three"></ListViewItem> </ListView.Items> </ListView>

3) Change DataTemplate from code behind

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; } }

enter image description here

Have updated my demo: LINK

更多推荐

本文发布于:2023-07-24 10:44:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1244933.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模板   ListView   selected   template

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!