There seems to be a problem with the SelectedItems of ListView when changing the ItemsPanel dynamically. I have implemented MVVM on a ListView whose ItemsSource is binded to a collection of Models. The Model has 2 properties, DisplayName(string) and Selected(bool). And the DataContext for the listview contains a ViewMode(bool) property.
The setup is that the IsSelected property of the ListViewItem is binded to the Selected property of the Model and the ListView's ItemsPanel changes when I changed the ViewMode by click a button.
The problem is that when there is a selected item in the ListView and the ViewMode is changed, the ListView's SelectedItems count increases, even if the selected items do not change.
Note: In my setup, there is only 1 item in the ListView but the SelectedItems count increaes everytime I changed the ViewMode.
Here's the xaml part of the application to test the problem. I think you experts can do the ViewModel/Model part.
<Window x:Class="WpfApplication5.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"
Width="525"
Height="350">
<StackPanel>
<Button Command="{Binding ChangeViewModeCommand}"
Content="Change ViewMode" />
<ListView x:Name="list" ItemsSource="{Binding Models}">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="IsSelected" Value="{Binding Path=Selected, Mode=TwoWay}" />
<Setter Property="Content" Value="{Binding DisplayName}" />
</Style>
</ListView.ItemContainerStyle>
<ListView.Style>
<!-- Default ItemsPanel -->
<Style TargetType="ListView">
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<StackPanel />
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<!-- Change ItemsPanel -->
<DataTrigger Binding="{Binding ViewMode}" Value="true">
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</ListView.Style>
</ListView>
<TextBlock Text="{Binding Path=SelectedItems.Count, ElementName=list, StringFormat=Selected Items Count:{0}}" />
</StackPanel>
</Window>
Edit
I'm adding the code for the ViewModel and the Model class. As you can see, it is as simple as it gets.
public class ViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public ObservableCollection<Model> Models { get; private set; }
private bool viewMode;
public bool ViewMode
{
get { return viewMode; }
set
{
if (viewMode != value)
{
viewMode = value;
OnPropertyChanged("ViewMode");
}
}
}
public ICommand ChangeViewModeCommand
{
get { return new DelegateCommand(() => ViewMode = ViewMode ? false : true); }
}
public ViewModel()
{
Models = new ObservableCollection<Model>();
Models.Add(new Model() { DisplayName = "Model1" });
}
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
Model Class
public class Model : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private bool isSelected;
public bool Selected
{
get { return isSelected; }
set
{
isSelected = value; OnPropertyChanged("Selected");
}
}
private string display;
public string DisplayName
{
get { return display; }
set { display = value; OnPropertyChanged("Display"); }
}
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
What is weird is that eventhough there is only 1 item in the Models collection, the ListView.SelectedItems.Count increases.
Thanks
SelectedItems.Count
is doing what it should. I would assume the problem must be in how you are binding to yourSelected(bool)
property. – Cadogi