[英]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? 根据是否选择了列表视图项,选择模板的最佳方法是什么?
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. 正确的方法是更改默认模板并在VisualState
自定义动画。
Copy and paste the default ListViewItem styles and templates in your project, see here 在项目中复制并粘贴默认的ListViewItem样式和模板,请参见此处
Change the brush in the Selected
and PointerOverSelected
visual states: 在Selected
和PointerOverSelected
视觉状态下更改画笔:
SystemControlHighlightListAccentLowBrush
in the Selected
visual state: 处于Selected
可视状态的SystemControlHighlightListAccentLowBrush
:
<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: PointerOverSelected
可视状态下的SystemControlHighlightListAccentMediumBrush
:
<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>
Check my completed sample here 在这里检查我完成的样本
-----Update(09/27/2016)----- -----更新(09/27/2016)-----
How would I use this to change the DataTemplate of a ListBoxItem when it's selected 选择列表框后,如何使用它来更改ListBoxItem的DataTemplate
If you need to switch DataTemplate
, you might change it from code behind. 如果需要切换DataTemplate
,则可以从后面的代码中进行更改。
1) Append a DataTeemplate in the page's resource: 1)在页面资源中附加一个DataTeemplate:
<Page.Resources>
<DataTemplate x:Key="dataTemplate1">
<StackPanel Orientation="Horizontal">
<TextBlock Text="->" />
<TextBlock Text="{Binding}" />
</StackPanel>
</DataTemplate>
</Page.Resources>
2) Add handler for SelectionChanged
event: 2)为SelectionChanged
事件添加处理程序:
<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 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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.