簡體   English   中英

選擇時ListView不同的模板

[英]ListView different template when selected

我正在嘗試為列表視圖項選擇一個不同的模板,但是我找不到有關此操作的任何信息。

根據是否選擇了列表視圖項,選擇模板的最佳方法是什么?

根據是否選擇了列表視圖項,選擇模板的最佳方法是什么?

正確的方法是更改​​默認模板並在VisualState自定義動畫。

  1. 在項目中復制並粘貼默認的ListViewItem樣式和模板,請參見此處

  2. SelectedPointerOverSelected視覺狀態下更改畫筆:

處於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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM