我的ListView用的SelectionMode =伸出和樣式的ListViewItem從而:樣式列表視圖Selector.SelectedItem
MainWindow.xaml:
<Window x:Class="ListViewSelection.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style TargetType="{x:Type ListViewItem}">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Aqua"/>
</Style.Resources>
<!--<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="Aqua" />
</Trigger>
</Style.Triggers>-->
</Style>
</Window.Resources>
<StackPanel>
<ListView Name="MyListView" ItemsSource="{Binding MyList}" SelectionChanged="SelectionChanged" SelectionMode="Extended" />
<Label Name="MyLabel" />
</StackPanel>
</Window>
MainWindow.xaml.cs:
using System.Windows;
using System.Windows.Controls;
using System.Collections.ObjectModel;
namespace ListViewSelection
{
public partial class MainWindow : Window
{
public ObservableCollection<string> MyList { get; set; }
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
MyList = new ObservableCollection<string>();
MyList.Add("Jane");
MyList.Add("Paul");
MyList.Add("Sally");
MyList.Add("Ian");
}
private void SelectionChanged(object sender, SelectionChangedEventArgs e)
{
MyLabel.Content = (sender as ListBox).SelectedItem;
}
}
}
這爲所有選定的項目設置顏色。但是我還需要選擇Selector.SelectedItem,它是選擇中的「活動」或第一項。這是標籤顯示的那個。
任何幫助?謝謝!
我瞭解使用數據觸發並綁定到基礎項目的想法,但在我的案件的ItemsSource在我的ListView勢必CollectionViewSource.View,所以不知道如何從那裏得到這個項目,否則即使我能修改它添加這個IsFirstInSelection屬性。 – DaveO 2011-01-28 09:22:53