在动态更改ItemsPanel时,ListView的SelectedItems似乎存在问题。我在ListView上实现了MVVM,其ItemsSource被绑定到一组Models。 Model有2个属性,DisplayName(字符串)和Selected(bool)。 listview的DataContext包含一个ViewMode(bool)属性。
设置是ListViewItem的IsSelected属性绑定到Model的Selected属性,当我通过单击按钮更改ViewMode时,ListView的ItemsPanel会更改。
问题在于,当ListView中有选定项目并且ViewMode发生更改时,ListView的SelectedItems计数会增加,即使所选项目没有更改也是如此。
注意:在我的设置中,ListView中只有1个项目,但每次更改ViewMode时,SelectedItems计数都会增加。
这是测试问题的应用程序的xaml部分。我认为你的专家可以做ViewModel / Model部分。
<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>
修改
我正在添加ViewModel和Model类的代码。如您所见,它就像它一样简单。
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));
}
模型类
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));
}
}
}
奇怪的是,尽管Models集合中只有1个项目,但ListView.SelectedItems.Count会增加。
由于