我想選擇全部項目在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");
}
}
綁定'ListViewItem.IsSelected'在您的項目視圖模型的一些布爾屬性,並更改該屬性對所有項目爲真 – dkozl
我已經在這裏回答了這個問題:[在列表視圖中選擇項目](http://stackoverflow.com/questions/35339524/how-can-i-get-all-selected-items-from-listview-in-viewmodel-class/35340701#35340701) –