我正在尝试为列表视图项目选择时创建一个不同的模板,但是我找不到任何关于这样做的信息。
根据是否选择了列表视图项,选择模板的最佳方法是什么?
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:
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; } }Have updated my demo: LINK
更多推荐
发布评论