dotnet / wpf

WPF is a .NET Core UI framework for building Windows desktop applications.
MIT License
7.03k stars 1.16k forks source link

SelectedItems property on DataGrid #3140

Open ice1e0 opened 4 years ago

ice1e0 commented 4 years ago

The DataGrid has a property SelectedItems property which can not be used in WPF like 'SelectedItem' because it has no dependency property. I would love to see this be added to WPF.

See as well: https://stackoverflow.com/questions/9880589/bind-to-selecteditems-from-datagrid-or-listbox-in-mvvm Here the DataGrid is used with SelectionMode "Extended" and using SelectionUnit="FullRow" --> with this configuration I think there should be no issue adding this functionality.

Here a raw template for a simple extension (only supporting to read the 'SelectedItems' property):

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)
        {
        }
}
DaveInCaz commented 4 years ago

I was going to open the same issue, glad it is already here.

Just to add, this has inspired MANY workarounds which should be a signal of how much of a problem it is. Examples:

Thanks

DGeiss2 commented 3 years ago

It would be realy great if you could implement the SelectedItems inside the DataGird/Listbox. Right now there is, as far as I know, just one solution, that works with virtualisation and a two way binding. See https://github.com/samueldjack/SelectedItemsBindingDemo This is the accepted solution from the https://stackoverflow.com/questions/9880589/bind-to-selecteditems-from-datagrid-or-listbox-in-mvvm , that already has been metioned by ice1e0

itsChris commented 3 years ago

i have been following this issue for years and it is difficult for me to understand why something so obviously needed is not there. Please expand the functionality so that not everyone has to build something themselves - thanks!

https://github.com/itsChris/WpfMvvmDataGridMultiselect

TysonMN commented 3 years ago

@itsChris, could you contribute a PR that adds a DependencyProperty for SelectedItems to MultiSelector?

DGeiss2 commented 2 years ago

How about https://docs.microsoft.com/en-us/dotnet/api/system.windows.controls.datagrid.selectedcellschanged ?

From: GFHuang @.> Sent: Saturday, August 20, 2022 3:53 PM To: dotnet/wpf @.> Cc: DGeiss2 @.>; Comment @.> Subject: Re: [dotnet/wpf] SelectedItems property on DataGrid (#3140)

I had try do define a attached property SelectedItemsCount for DataGrid, my idea is to subscribe DataGrid.SelectionChanged, then raise SelectedItemsCount changed event (maybe set the DataGrid.SelectedItems.Count to SelectedItemsCount for this purpose). But I can't find any entry point for subscribing DataGrid.SelectionChanged, does anyone have a good idea?

— Reply to this email directly, view it on GitHubhttps://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fdotnet%2Fwpf%2Fissues%2F3140%23issuecomment-1221318933&data=05%7C01%7Cdgeiss%40kip.com%7Cbe08f292ac04418eade608da82b35764%7Cb965b6026aa642fe8b9936a0790307d1%7C0%7C0%7C637966004052259393%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=FiaQCdgvYGvrR9VxTMUxgW%2FzsfLxD0t3wPva96nFgws%3D&reserved=0, or unsubscribehttps://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FAOU5277AZWYDRBFMDHL47BTV2DPMVANCNFSM4N5N3BEQ&data=05%7C01%7Cdgeiss%40kip.com%7Cbe08f292ac04418eade608da82b35764%7Cb965b6026aa642fe8b9936a0790307d1%7C0%7C0%7C637966004052415623%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=m3MFgxZSTMBRX9l86Q4mTGVulf3TZ%2B3Fxwn7ux%2F%2FROI%3D&reserved=0. You are receiving this because you commented.Message ID: @.***>

DaveInCaz commented 8 months ago

@ice1e0 I suggest modifying the title of this issue to mention ListBox and/or ListView as well as DataGrid. Just because those others are also very common examples of this problem and that may make it easier for people to find this issue.

image

and

image

The actual problem may be in the Selector or MultiSelector base class but I doubt many people would actually search for that. (?)