我的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