【发布时间】:2017-11-23 05:32:59
【问题描述】:
单击列表视图标题中的复选框时,我正在尝试选择/取消选择所有列表视图项目。
查看 (xaml):
<ListView Margin="10" Name="MyLv" ItemsSource="Binding Path=lstData}" SelectionMode="Extended">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="IsSelected" Value="{Binding IsSelected}"/>
</Style>
</ListView.ItemContainerStyle>
<ListView.View>
<GridView>
<!-- Checkbox column -->
<GridViewColumn>
<GridViewColumn.Header>
<CheckBox x:Name="CheckAll" Command="{Binding CheckAllCommand}"
CommandParameter="{Binding IsChecked, ElementName=CheckAll}" />
</GridViewColumn.Header>
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding IsSelected}" />
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="ID" Width="120" DisplayMemberBinding="{Binding ID}" />
<GridViewColumn Header="Desc" Width="50" DisplayMemberBinding="{Binding Desc}" />
</GridView>
</ListView.View>
</ListView>
代码隐藏构造函数 (xaml.cs):
public MyView()
{
DataContext = new MyViewModel();
InitializeComponent();
}
视图模型:
public ObservableCollection<DataModel> lstData = null;
public MyViewModel()
{
this.lstData = this.LoadData(); // this connects to a database an extract info to be loaded in listview
}
private RelayCommand checkAllCommand;
public ICommand CheckAllCommand
{
get
{
return checkAllCommand ??
(checkAllCommand = new RelayCommand(param => this.SelectUnselectAll(Convert.ToBoolean(param.ToString()))));
}
}
private void SelectUnselectAll(bool isSelected)
{
for (int i = 0; i < this.lstData.Count; i++)
{
this.lstData[i].IsSelected = isSelected;
}
}
数据模型:
public class DataModel
{
public bool IsSelected { get; set; }
public string ID { get; set; }
public string Desc { get; set; }
}
RelayCommand 类:
public class RelayCommand : ICommand
{
#region Fields
readonly Action<object> _execute;
readonly Predicate<object> _canExecute;
#endregion // Fields
#region Constructors
/// <summary>
/// Creates a new command that can always execute.
/// </summary>
/// <param name="execute">The execution logic.</param>
public RelayCommand(Action<object> execute)
: this(execute, null)
{
}
/// <summary>
/// Creates a new command.
/// </summary>
/// <param name="execute">The execution logic.</param>
/// <param name="canExecute">The execution status logic.</param>
public RelayCommand(Action<object> execute, Predicate<object> canExecute)
{
if (execute == null)
throw new ArgumentNullException("execute");
_execute = execute;
_canExecute = canExecute;
}
#endregion // Constructors
#region ICommand Members
[DebuggerStepThrough]
public bool CanExecute(object parameter)
{
return _canExecute == null ? true : _canExecute(parameter);
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public void Execute(object parameter)
{
_execute(parameter);
}
#endregion // ICommand Members
}
我的问题如下:当我选中/取消选中列表视图标题中的复选框时,每个列表视图项的列表视图中的 IsSelected 列不会更新。我想要以下行为:
- 如果我选中列表视图标题中的复选框,则将选中所有列表视图项。
- 如果我取消选中列表视图标题中的复选框,所有列表视图项都将被取消选中。
【问题讨论】:
-
您的 DataModel 必须为 IsSelected 属性触发 PropertyChanged 事件,即实现 INotifyPropertyChanged。
-
@Clemens 是的,我已经实现了,我在这里指定了。当你说我必须触发 PropertyChanged 事件时,在哪里?在每次迭代的方法 SelectUnselectAll 中,还是在方法结束时一次?
-
你只有
public bool IsSelected { get; set; }。这不会触发 PropertyChanged 事件。 -
@Clemens 我在 viewmodel 中完成了这项工作。
-
当您更改集合中对象的属性时,您并没有对集合进行更改。
标签: wpf listview mvvm binding listviewitem