2011-01-28 88 views
0

我的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,它是選擇中的「活動」或第一項。這是標籤顯示的那個。

任何幫助?謝謝!

回答

0

你可以暴露你的ViewData類IsFirstInSelection屬性(我想你有的話)。

然後,你可以把DataTrigger監測這樣的變化:

 <Style.Triggers> 
      <DataTrigger Binding="{Binding IsFirstInSelection}"> 
       <Setter Property="Background" 
         Value="Blue"/> 
      </DataTrigger> 
     </Style.Triggers> 

另外,在你的情況我建議你避免改變HighlightBrushKey,但實現通過改變背景屬性你的目標。你也可能使IsSelected屬性中的ViewData類,所以它看起來是:

public class ViewData: ObservableObject 
{ 
    private bool _isSelected; 
    public bool IsSelected 
    { 
     get 
     { 
      return this._isSelected; 
     } 
     set 
     { 
      if (this._isSelected != value) 
      { 
       this._isSelected = value; 
       this.OnPropertyChanged("IsSelected"); 
      } 
     } 
    } 

    private bool _isFirstInSelection; 
    public bool IsFirstInSelection 
    { 
     get 
     { 
      return this._isFirstInSelection; 
     } 
     set 
     { 
      if (this._isFirstInSelection != value) 
      { 
       this._isFirstInSelection = value; 
       this.OnPropertyChanged("IsFirstInSelection"); 
      } 
     } 
    } 
} 

而基於這兩個屬性的樣式集背景:

 <Style x:Key="TreeItemStyle" 
       TargetType="{x:Type TreeViewItem}"> 
      <Setter Property="IsSelected" 
        Value="{Binding Path=IsSelected, Mode=TwoWay}"/> 
      <Setter Property="IsFirstInSelection" 
        Value="{Binding Path=IsFirstInSelection}"/> 
      <Setter Property="Background" 
        Value="Green"/> 
      <Style.Triggers> 
       <DataTrigger Binding="{Binding IsSelected}" 
          Value="True"> 
        <Setter Property="Background" 
          Value="Red"/> 
       </DataTrigger> 
       <DataTrigger Binding="{Binding IsFirstInSelection}"> 
        <Setter Property="Background" 
         Value="Blue"/> 
       </DataTrigger> 
      </Style.Triggers> 
     </Style> 
在模型或視圖代碼後面,你應該

當然監視IsSelected屬性已更改通知並根據選定項目設置/重置IsFirstInSelection。

我希望你能理解我的想法。

------- -------- EDITED

起初我繼續堅持,你應該養成正確的ViewData類:

public class ViewData : INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 
    protected void OnPropertyChanged(string propertyName) 
    { 
     var ev = this.PropertyChanged; 
     if (ev != null) 
     { 
      ev(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 

    public ViewData(string name) 
    { 
     this._personName = name; 
    } 

    private string _personName; 
    public string PersonName 
    { 
     get 
     { 
      return this._personName; 
     } 
     set 
     { 
      if (this._personName != value) 
      { 
       this._personName = value; 
       this.OnPropertyChanged("PersonName"); 
      } 
     } 
    } 

    private bool _isFirstSelected; 
    public bool IsFirstSelected 
    { 
     get 
     { 
      return this._isFirstSelected; 
     } 
     set 
     { 
      if (this._isFirstSelected != value) 
      { 
       this._isFirstSelected = value; 
       this.OnPropertyChanged("IsFirstSelected"); 
      } 
     } 
    } 
} 

在處理的SelectionChanged事件你應該修改屬性IsFirstSelected像這樣:

public ObservableCollection<ViewData> MyList { get; set; } 
private void SelectionChanged(object sender, SelectionChangedEventArgs e) 
{ 
    var lb = sender as ListBox; 
    if (lb != null) 
    { 
     MyLabel.Content = lb.SelectedItem; 
     foreach (var item in this.MyList) 
     { 
      if (item == lb.SelectedItem) 
      { 
       item.IsFirstSelected = true; 
      } 
      else 
      { 
       item.IsFirstSelected = false; 
      } 
     } 
    } 
} 

但是默認的ListViewItem模板不會對Background setters所以你應該覆蓋其風格的模板:

<Style TargetType="{x:Type ListViewItem}"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="ListViewItem"> 
       <Grid Background="{TemplateBinding Background}"> 
        <ContentPresenter Content="{Binding PersonName}"/> 
       </Grid> 
       <ControlTemplate.Triggers> 
        <Trigger Property="IsSelected" 
           Value="True"> 
         <Setter Property="Background" 
           Value="Yellow"/> 
        </Trigger> 

        <DataTrigger Binding="{Binding IsFirstSelected}" 
            Value="True"> 
         <Setter Property="Background" 
           Value="Aqua" /> 
        </DataTrigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

如果這種解決方案是不可接受的,請告訴我。我有一些想法,但沒有足夠的時間在代碼中檢查它們。

+0

我瞭解使用數據觸發並綁定到基礎項目的想法,但在我的案件的ItemsSource在我的ListView勢必CollectionViewSource.View,所以不知道如何從那裏得到這個項目,否則即使我能修改它添加這個IsFirstInSelection屬性。 – DaveO 2011-01-28 09:22:53