2016-02-13 168 views
1

我想選擇全部項目ListView,使用mvvm。使用代碼隱藏,我試過SelectAll()foreach選擇它們,但我想使用MVVM像我的項目的其餘部分。有任何想法嗎?在MVVM中選擇所有ListView項目

這裏是列表視圖:

<ListView x:Name="TransformerList" ItemsSource="{Binding CurrentStations}" Margin="16,250,0,10.4" SelectionMode="Multiple" HorizontalAlignment="Left" Width="411"> 
     <i:Interaction.Triggers> 
      <i:EventTrigger EventName="Select"> 
       <i:InvokeCommandAction Command="{Binding SeeAllCustomersCommand}" CommandParameter="{Binding Item1}"/> 
      </i:EventTrigger> 
      <i:EventTrigger EventName="SelectionChanged"> 
       <i:InvokeCommandAction Command="{Binding SelectedCustomersChangedCommand}" /> 
      </i:EventTrigger> 
     </i:Interaction.Triggers> 
     <ListView.ItemContainerStyle> 
      <Style TargetType="{x:Type ListViewItem}"> 
       <Setter Property="IsSelected" Value="{Binding Mode=TwoWay, Path=IsSelected}" /> 
       <Setter Property="IsSelected" Value="{Binding Mode=TwoWay, Path=TransformerIsSelected}" /> 
      </Style> 
     </ListView.ItemContainerStyle> 
     <ListView.View> 
      <GridView> 
       <GridViewColumn Header="Nettstasjon" Width="70" DisplayMemberBinding="{Binding Path=Name}" /> 
       <GridViewColumn Header="Område" Width="210" DisplayMemberBinding="{Binding Path=Area}"/> 
       <GridViewColumn Header="Radial" Width="110" DisplayMemberBinding="{Binding Path=Radial}"/> 
      </GridView> 
     </ListView.View> 
    </ListView> 

和按鈕:

public ICommand cmd_VelgAlle { get { return new RelayCommand(on_cmd_VelgAlle); } } 
    private void on_cmd_VelgAlle() 
    { 
     foreach (item i in CurrentStations) //the item here gives an error "type or namespace could not be found" 
     { 
      i.TransformerIsSelected = true; 
     } 
    } 

而且MVVM:

private bool _TransformerIsSelected; 
    public bool TransformerIsSelected 
    { 
     get { return _TransformerIsSelected; } 
     set 
     { 
      _TransformerIsSelected = value; 
      RaisePropertyChanged("TransformerIsSelected"); 
     } 
    } 
+0

綁定'ListViewItem.IsSelected'在您的項目視圖模型的一些布爾屬性,並更改該屬性對所有項目爲真 – dkozl

+1

我已經在這裏回答了這個問題:[在列表視圖中選擇項目](http://stackoverflow.com/questions/35339524/how-can-i-get-all-selected-items-from-listview-in-viewmodel-class/35340701#35340701) –

回答

1

你需要創建一個ListViewItemStyle

<ListView.ItemContainerStyle> 
    <Style TargetType="ListViewItem"> 
     <Setter Property="IsSelected" Value="{Binding IsSelected}" /> 
    </Style> 
</ListView.ItemContainerStyle> 

Model類創建一個屬性:

public bool IsSelected 
{ 
    get { return isSelected; } 
    set 
    { 
     isSelected = value; 
     RaiseChange("IsSelected"); 
    } 
} 

並重復你的ItemSource設置IsSelectedtrue對所有項目(在視圖模型

foreach(item i in yourCollection) 
{ 
    i.IsSelected = true; 
} 

Upda TE:

使用樣式爲:

<ListView.ItemContainerStyle> 
     <Style TargetType="{x:Type ListViewItem}">     
      <Setter Property="IsSelected" Value="{Binding Mode=TwoWay, Path=TransformerIsSelected}" /> 
     </Style> 
</ListView.ItemContainerStyle> 

命令:

private void on_cmd_VelgAlle() 
    { 
     //Item is the class your CurrentStations is made of(i guess something like `station` in your design) 
     //i.e if CurrentStations is list of string then Item will be string 
     foreach (Item i in CurrentStations) 
     { 
      i.TransformerIsSelected = true; 
     } 
    } 
+0

謝謝你的回答。我目前正試圖實現這一點,但我遇到的問題與foreach。我似乎無法讓物品上班。你的收藏將無法工作,無論我放在那裏。我應該從XAML或ItemSource中輸入x:Name嗎?有小費嗎? – Thomas

+0

@Thomas YourCollection是listView的itemSource。您還需要在Model類中創建IsSelected屬性(即,無論您的集合是由哪個類構成的) –

+0

我將編輯該問題以包含一些代碼,以便您能夠看到:)再次感謝您的幫助。 – Thomas