In my application I have a listview with an item container style bound to the IsSelected property on my view model.
I have also set up an inputbinding on the listview to handle selecting all items in the list programatically, since the default does not work due to the virtualizing stack panel. This works well and good.
The problem arises when the user clicks on a single list item after having pressed CTRL+A. What the user should expect to happen is the single new item clicked on becomes the only item selected. What actually happens is the listview does not update the IsSelected property for items out of sight, and only the items currently visible become unselected.
How can I handle this behavior properly?
<ListView
Name="sortList"
Grid.Row="1"
ItemsSource="{Binding RelativeSource={RelativeSource
FindAncestor, AncestorType={x:Type UserControl}},
Path=ItemsSource, Mode=TwoWay}">
<ListView.InputBindings>
<KeyBinding Gesture="CTRL+A" Command="{Binding SelectAllCommand}" />
</ListView.InputBindings>
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="Padding" Value="3" />
<Setter
Property="IsSelected"
Value="{Binding Path=IsSelected, Mode=TwoWay}" />
</Style>
</ListView.ItemContainerStyle>
</ListView>
Here is the select all command.
private RelayCommand _selectAllCommand;
public System.Windows.Input.ICommand SelectAllCommand
{
get
{
if (_selectAllCommand == null)
_selectAllCommand = new RelayCommand(param => this.SelectAll());
return _selectAllCommand;
}
}
private void SelectAll()
{
foreach (object o in this.Objects)
if (!this.SelectedItems.Contains(order))
order.IsSelected = true;
}