WPF MVVM:selecting/unselecting 所有列表视图项的列表视图复选框 header
WPF MVVM: Listview checkbox header for selecting/unselecting all the listview items
我正在尝试 select/unselect 单击列表视图 header 中的复选框时的所有列表视图项目。
查看 (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>
Code-Behind构造函数(xaml.cs):
public MyView()
{
DataContext = new MyViewModel();
InitializeComponent();
}
ViewModel:
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; }
}
中继命令Class:
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
}
我的问题如下:当我 check/uncheck 列表视图 header 中的复选框时,列表视图中每个 listviewitem 的 IsSelected 列未更新。我想要以下行为:
- 如果我选中列表视图中的复选框 header,所有列表视图项目都将被选中。
- 如果我取消选中列表视图中的复选框 header,所有列表视图项目都将被取消选中。
您的 class DataModel 必须实现 INotifyPropertyChanged
,并在 IsSelected
属性 更改时触发 PropertyChanged
事件。否则,不会通知 ListViewItem 的 IsSelected
属性 的绑定。
public class DataModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private bool isSelected;
public bool IsSelected
{
get { return isSelected; }
set
{
isSelected = value;
PropertyChanged?.Invoke(this,
new PropertyChangedEventArgs(nameof(IsSelected)));
}
}
public string ID { get; set; }
public string Desc { get; set; }
}
我正在尝试 select/unselect 单击列表视图 header 中的复选框时的所有列表视图项目。
查看 (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>
Code-Behind构造函数(xaml.cs):
public MyView()
{
DataContext = new MyViewModel();
InitializeComponent();
}
ViewModel:
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; }
}
中继命令Class:
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
}
我的问题如下:当我 check/uncheck 列表视图 header 中的复选框时,列表视图中每个 listviewitem 的 IsSelected 列未更新。我想要以下行为:
- 如果我选中列表视图中的复选框 header,所有列表视图项目都将被选中。
- 如果我取消选中列表视图中的复选框 header,所有列表视图项目都将被取消选中。
您的 class DataModel 必须实现 INotifyPropertyChanged
,并在 IsSelected
属性 更改时触发 PropertyChanged
事件。否则,不会通知 ListViewItem 的 IsSelected
属性 的绑定。
public class DataModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private bool isSelected;
public bool IsSelected
{
get { return isSelected; }
set
{
isSelected = value;
PropertyChanged?.Invoke(this,
new PropertyChangedEventArgs(nameof(IsSelected)));
}
}
public string ID { get; set; }
public string Desc { get; set; }
}