【发布时间】:2012-11-27 18:24:43
【问题描述】:
我的 WPF 窗口上有一个 ListView,并且我有一个按钮可以全选。首先,如何让按钮选择列表视图中的所有项目。
其次,我需要我的 ViewModel 来检查所有选定的项目。如何在我的 ViewModel 中获取这些信息?
我已经读过您可以使用 IsSelected 属性执行此操作,但有一个错误,即本地属性会覆盖绑定属性,因此如果之前已经选择了它,那么它似乎不会再次被选择 - 或类似的东西.看起来很复杂。 The blog that looks into this problem
然后我读了这个博客Data binding to selected items ,看起来也很复杂。
我想知道它是否必须如此复杂,并且这些示例是唯一的前进方向。
XAML:
<ListView Name="sources_ListView" Grid.RowSpan="1" ItemsSource="{Binding Path=Sources}">
<ListView.View>
<GridView>
<GridViewColumn Width="290" Header="Name">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=OriginalPath}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Width="80" Header="Type">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Type}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
<Button Grid.Row="0" Grid.Column="0" Content="Select All" Name="selectAllSources_Button" Margin="3" />
<Button Grid.Row="0" Grid.Column="1" Content="Deselect All" Name="deselectAllSources_Button" Margin="3" />
<Button Grid.Row="0" Grid.Column="3" Content="Remove Selected" Name="removeSelected_Button" Margin="3" Width="100" HorizontalAlignment="Right" />
【问题讨论】:
标签: c# wpf mvvm visual-studio-2012