繁体   English   中英

Wpf MutliSelect DataGrid 与自定义数据网格

[英]Wpf MutliSelect DataGrid with custom datagrid

我知道有几篇关于如何实现数据网格多选的帖子。

我在 Github ( https://github.com/dotnet/wpf/issues/3140 ) 上找到了未解决的问题。 用户 ice1e0 提到了一个带有CustomDataGrid class 的解决方案。

public class CustomDataGrid : DataGrid
{
        public static readonly DependencyProperty SelectedItemsProperty = DependencyProperty.Register(nameof(SelectedItems), typeof(IList), typeof(CustomDataGrid), new PropertyMetadata(default(IList), OnSelectedItemsPropertyChanged));

        protected override void OnSelectionChanged(SelectionChangedEventArgs e)
        {
            base.OnSelectionChanged(e);
            SetValue(SelectedItemsProperty, base.SelectedItems);
        }

        public new IList SelectedItems
        {
            get => (IList)GetValue(SelectedItemsProperty);
            set => throw new Exception("This property is read-only. To bind to it you must use 'Mode=OneWayToSource'.");
        }

        private static void OnSelectedItemsPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((CustomDataGrid)d).OnSelectedItemsChanged((IList)e.OldValue, (IList)e.NewValue);
        }

        protected virtual void OnSelectedItemsChanged(IList oldSelectedItems, IList newSelectedItems)
        {
        }
}

所以,我尝试实现它,所以我复制了 class。 我的 xaml 看起来像这样:

<Project1:CustomDataGrid ItemsSource="{Binding Data}" SelectionMode="Extended" SelectionUnit="FullRow" IsReadOnly="True" SelectedItems="{Binding SelectedComputers, Mode=OneWayToSource}" />

和我的视图模型:

[ObservableProperty]
ObservableCollection<string> _selectedComputers;

我使用 CommunityToolkit.Mvvm (v8.0)。

它编译但SelectedComputers是 null,经过我 select 一项或多项。

更新:

这个问题似乎

        void CustomDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            this.SelectedItemsList = this.SelectedItems;
        }

不起作用。 SelectedItemsList始终为 null。 另一个线程中提到了该问题: Select multiple items from a DataGrid in an MVVM WPF project

M463建议的解决方案:

this.SelectedItemsList = this.SelectedItems; 对我不起作用,因为 SelectedItemsList 始终设置为 null。 但是,将代码更改为 foreach (var item in this.SelectedItems) { this.SelectedItemsList.Add(item); } 成功了。 请注意,这需要您调用 this.SelectedItemsList.Clear(); 提前,所以 SelectedItemsList 的项目不会被重复。

当我尝试解决方案时,在选择一行后,应用程序崩溃并出现错误:

System.ArgumentException: 'The value "System.Data.DataRowView" is not of type "System.String" and cannot be used in this generic collection.
Parameter name: value'

您不能混合类型。

如果您DataDataView ,则SelectedItems不能绑定到ObservableCollection<string>因为您不能将(选定的) DataRowView添加到ObservableCollection<string>

此外,您不能将ObservableCollection<string>属性设置为 ObservableCollection<string ObservableCollection<string>以外的任何其他属性。 这就是为什么当您尝试使用SetValue将其设置为IList时,源属性的设置器中的值以null的形式出现。

如果您将SelectedComputers设为IList ,您应该会看到它是从控件中设置的:

[ObservableProperty]
IList _selectedComputers;

另一种选择显然是更改依赖属性的类型。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM