如何获取 DataGrid 中选定行的集合? MVVM WPF
How can I get a collection of selected rows in the DataGrid? MVVM WPF
下午好,
我正在尝试获取 DataGrid 中选定行的列表,但是 SelectedItem returns 只是选定的行,而不是它们的列表。 =[
我在我的 WPF 项目中将 Caliburn Micro 用于 MVVM。
---这是我的数据网格---
<DataGrid Margin="5" AutoGenerateColumns="False" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"
CanUserAddRows="False" SelectionMode="Extended" MinHeight="320"
SelectedItem="{Binding Path=SelectedRows, Mode=OneWayToSource}" x:Name="Avisos" SelectionUnit="FullRow">
<DataGrid.Columns>
<DataGridTextColumn Header="Inquilino" Binding="{Binding Inquilino}" MinWidth="150"/>
<DataGridTextColumn Header="Propietário" Binding="{Binding Propietario}" MinWidth="150"/>
<DataGridTextColumn Header="Imovel" Binding="{Binding Imovel}" MinWidth="150"/>
<DataGridTextColumn Header="Data Aviso" Binding="{Binding DataLembrete}" MinWidth="150"/>
<DataGridTextColumn Header="Status" Binding="{Binding Status}" MinWidth="70"/>
<DataGridTextColumn Header="Descrição" Binding="{Binding Descrição}" MinWidth="150" Width="*"/>
</DataGrid.Columns>
<DataGrid>
---这是我在 ViewModel 上的列表---
public ObservableCollection<AvisoModel> SelectedRows { get; set; }
---我得到这个错误---
Exceção gerada: 'System.NotSupportedException' em PresentationFramework.dll
System.Windows.Data Error: 7 : ConvertBack cannot convert value 'MiniAgendadaTay.Models.AvisoModel' (type 'AvisoModel'). BindingExpression:Path=SelectedRows; DataItem='MainWindowViewModel' (HashCode=10588721); target element is 'DataGrid' (Name='Avisos'); target property is 'SelectedItem' (type 'Object') NotSupportedException:'System.NotSupportedException: CollectionConverter cannot convert from MiniAgendadaTay.Models.AvisoModel.
at MS.Internal.Data.DefaultValueConverter.ConvertHelper(Object o, Type destinationType, DependencyObject targetElement, CultureInfo culture, Boolean isForward)
at MS.Internal.Data.ObjectTargetConverter.ConvertBack(Object o, Type type, Object parameter, CultureInfo culture)
at System.Windows.Data.BindingExpression.ConvertBackHelper(IValueConverter converter, Object value, Type sourceType, Object parameter, CultureInfo culture)'
您可以定义为使用 DataGrid 的 SelectedItems 属性 的行为。 SelectedItems,不是 DepedencyProperty,这就是为什么你不能现成的。
public class DataGridSelectedItemsBehavior:Behavior<DataGrid>
{
protected override void OnAttached()
{
AssociatedObject.SelectionChanged += SelectionChanged;
}
protected override void OnDetaching()
{
AssociatedObject.SelectionChanged -= SelectionChanged;
}
void SelectionChanged(object sender, SelectionChangedEventArgs e)
{
SelectedItems = AssociatedObject.SelectedItems;
}
public static readonly DependencyProperty SelectedItemsProperty =
DependencyProperty.Register(nameof(SelectedItems), typeof(IEnumerable), typeof(DataGridSelectedItemsBehavior),
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
public IList SelectedItems
{
get { return (IList)GetValue(SelectedItemsProperty); }
set { SetValue(SelectedItemsProperty, value); }
}
}
现在您可以在 Xaml 中添加行为
<i:Interaction.Behaviors>
<behaviors:DataGridSelectedItemsBehavior SelectedItems="{Binding SelectedItems, Mode=OneWayToSource}" />
</i:Interaction.Behaviors>
并且在您的 ViewModel 中
public IEnumerable<object> SelectedItems { get; set; }
下午好, 我正在尝试获取 DataGrid 中选定行的列表,但是 SelectedItem returns 只是选定的行,而不是它们的列表。 =[
我在我的 WPF 项目中将 Caliburn Micro 用于 MVVM。
---这是我的数据网格---
<DataGrid Margin="5" AutoGenerateColumns="False" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"
CanUserAddRows="False" SelectionMode="Extended" MinHeight="320"
SelectedItem="{Binding Path=SelectedRows, Mode=OneWayToSource}" x:Name="Avisos" SelectionUnit="FullRow">
<DataGrid.Columns>
<DataGridTextColumn Header="Inquilino" Binding="{Binding Inquilino}" MinWidth="150"/>
<DataGridTextColumn Header="Propietário" Binding="{Binding Propietario}" MinWidth="150"/>
<DataGridTextColumn Header="Imovel" Binding="{Binding Imovel}" MinWidth="150"/>
<DataGridTextColumn Header="Data Aviso" Binding="{Binding DataLembrete}" MinWidth="150"/>
<DataGridTextColumn Header="Status" Binding="{Binding Status}" MinWidth="70"/>
<DataGridTextColumn Header="Descrição" Binding="{Binding Descrição}" MinWidth="150" Width="*"/>
</DataGrid.Columns>
<DataGrid>
---这是我在 ViewModel 上的列表---
public ObservableCollection<AvisoModel> SelectedRows { get; set; }
---我得到这个错误---
Exceção gerada: 'System.NotSupportedException' em PresentationFramework.dll System.Windows.Data Error: 7 : ConvertBack cannot convert value 'MiniAgendadaTay.Models.AvisoModel' (type 'AvisoModel'). BindingExpression:Path=SelectedRows; DataItem='MainWindowViewModel' (HashCode=10588721); target element is 'DataGrid' (Name='Avisos'); target property is 'SelectedItem' (type 'Object') NotSupportedException:'System.NotSupportedException: CollectionConverter cannot convert from MiniAgendadaTay.Models.AvisoModel. at MS.Internal.Data.DefaultValueConverter.ConvertHelper(Object o, Type destinationType, DependencyObject targetElement, CultureInfo culture, Boolean isForward) at MS.Internal.Data.ObjectTargetConverter.ConvertBack(Object o, Type type, Object parameter, CultureInfo culture) at System.Windows.Data.BindingExpression.ConvertBackHelper(IValueConverter converter, Object value, Type sourceType, Object parameter, CultureInfo culture)'
您可以定义为使用 DataGrid 的 SelectedItems 属性 的行为。 SelectedItems,不是 DepedencyProperty,这就是为什么你不能现成的。
public class DataGridSelectedItemsBehavior:Behavior<DataGrid>
{
protected override void OnAttached()
{
AssociatedObject.SelectionChanged += SelectionChanged;
}
protected override void OnDetaching()
{
AssociatedObject.SelectionChanged -= SelectionChanged;
}
void SelectionChanged(object sender, SelectionChangedEventArgs e)
{
SelectedItems = AssociatedObject.SelectedItems;
}
public static readonly DependencyProperty SelectedItemsProperty =
DependencyProperty.Register(nameof(SelectedItems), typeof(IEnumerable), typeof(DataGridSelectedItemsBehavior),
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
public IList SelectedItems
{
get { return (IList)GetValue(SelectedItemsProperty); }
set { SetValue(SelectedItemsProperty, value); }
}
}
现在您可以在 Xaml 中添加行为
<i:Interaction.Behaviors>
<behaviors:DataGridSelectedItemsBehavior SelectedItems="{Binding SelectedItems, Mode=OneWayToSource}" />
</i:Interaction.Behaviors>
并且在您的 ViewModel 中
public IEnumerable<object> SelectedItems { get; set; }