WPF DataGrid多重选择绑定 [英] WPF DataGrid multiselect binding
问题描述
Andy是正确的。 DataGridRow.IsSelected 是一个依赖属性,可以通过数据绑定来控制ViewModel中的选择。以下示例代码演示了这一点:
< Window x:Class =DataGridMultiSelectSample.Window1
xmlns = http://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns:x =http://schemas.microsoft.com/winfx/2006/xaml
xmlns:tk =clr-namespace:Microsoft.Windows.Controls; assembly = WPFToolkit
Title =Window1Height =300Width =300>
< StackPanel>
< tk:DataGrid AutoGenerateColumns =FalseItemsSource ={Binding}EnableRowVirtualization =False>
< tk:DataGrid.Columns>
< tk:DataGridTextColumn Header =ValueBinding ={Binding Value}/>
< / tk:DataGrid.Columns>
< tk:DataGrid.RowStyle>
< Style TargetType =tk:DataGridRow>
< Setter Property =IsSelectedValue ={Binding IsSelected,Mode = TwoWay}/>
< / Style>
< / tk:DataGrid.RowStyle>
< / tk:DataGrid>
< Button Content =选择偶数Click =Even_Click/>
< Button Content =选择奇数Click =Odd_Click/>
< / StackPanel>
< / Window>
System.ComponentModel;
使用System.Windows;
命名空间DataGridMultiSelectSample
{
public partial class Window1
{
public Window1()
{
InitializeComponent();
DataContext = new []
{
new MyViewModel {Value =Able},
new MyViewModel {Value =Baker},
new MyViewModel {Value =Charlie},
new MyViewModel {Value =Dog},
new MyViewModel {Value =Fox},
};
}
private void Even_Click(object sender,RoutedEventArgs e)
{
var array =(MyViewModel [])DataContext; (int i = 0; i array [i] .IsSelected = i%2 == 0;
}
private void Odd_Click(object sender,RoutedEventArgs e)
{
var array =(MyViewModel [])DataContext; (int i = 0; i array [i] .IsSelected = i%2 == 1;
}
}
public class MyViewModel:INotifyPropertyChanged
{
public string Value {get;组; }
private bool mIsSelected;
public bool IsSelected
{
get {return mIsSelected; }
set
{
if(mIsSelected == value)return;
mIsSelected = value;
if(PropertyChanged!= null)
PropertyChanged(this,new PropertyChangedEventArgs(IsSelected));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
}
请确保设置 EnableRowVirtualisation =False
上的DataGrid元素,否则存在IsSelected绑定脱离kilter的风险。
I have a datagrid that is multi-select enabled. I need to change the selection in the viewmodel. However, the SelectedItems property is read only and can't be directly bound to a property in the viewmodel. So how do I signal to the view that the selection has changed?
Andy is correct. DataGridRow.IsSelected is a Dependency Property that can be databound to control selection from the ViewModel. The following sample code demonstrates this:
<Window x:Class="DataGridMultiSelectSample.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:tk="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit"
Title="Window1" Height="300" Width="300">
<StackPanel>
<tk:DataGrid AutoGenerateColumns="False" ItemsSource="{Binding}" EnableRowVirtualization="False">
<tk:DataGrid.Columns>
<tk:DataGridTextColumn Header="Value" Binding="{Binding Value}" />
</tk:DataGrid.Columns>
<tk:DataGrid.RowStyle>
<Style TargetType="tk:DataGridRow">
<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
</Style>
</tk:DataGrid.RowStyle>
</tk:DataGrid>
<Button Content="Select Even" Click="Even_Click" />
<Button Content="Select Odd" Click="Odd_Click" />
</StackPanel>
</Window>
using System.ComponentModel;
using System.Windows;
namespace DataGridMultiSelectSample
{
public partial class Window1
{
public Window1()
{
InitializeComponent();
DataContext = new[]
{
new MyViewModel {Value = "Able"},
new MyViewModel {Value = "Baker"},
new MyViewModel {Value = "Charlie"},
new MyViewModel {Value = "Dog"},
new MyViewModel {Value = "Fox"},
};
}
private void Even_Click(object sender, RoutedEventArgs e)
{
var array = (MyViewModel[]) DataContext;
for (int i = 0; i < array.Length; ++i)
array[i].IsSelected = i%2 == 0;
}
private void Odd_Click(object sender, RoutedEventArgs e)
{
var array = (MyViewModel[])DataContext;
for (int i = 0; i < array.Length; ++i)
array[i].IsSelected = i % 2 == 1;
}
}
public class MyViewModel : INotifyPropertyChanged
{
public string Value { get; set; }
private bool mIsSelected;
public bool IsSelected
{
get { return mIsSelected; }
set
{
if (mIsSelected == value) return;
mIsSelected = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("IsSelected"));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
}
Be sure to set EnableRowVirtualisation="False"
on the DataGrid element, else there's a risk that the IsSelected bindings fall out of kilter.
这篇关于WPF DataGrid多重选择绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!