xceedsoftware / wpftoolkit

All the controls missing in WPF. Over 1 million downloads.
Other
3.9k stars 878 forks source link

NullReferenceException when SelectAll for collection of value types #1787

Open darxis opened 3 weeks ago

darxis commented 3 weeks ago

A NullReferenceException is thrown when the SelectAll/UnselectAll methods within SelectAllSelector are called and the SelectedItems collection items type is a value type.

For example you could have CheckComboBox with enums as items. When clicking the Select all checkbox a NullReferenceException is thrown.

XceedBoucherS commented 3 weeks ago

Hi,

How exactly do you define the CheckComboBox items to use enums ? If I use the following, I don't have the NullRefException: public enum MyData { First, Second, Third, Fourth } _myCheckCombo.ItemsSource = new List<MyData>() { MyData.First, MyData.Second, MyData.Third, MyData.Fourth, };

Thank you

darxis commented 3 weeks ago

You need to bind SelectedItems and then call SelectAll/UnselectAll by clicking the Select all/Unselect all entry in the UI, then it will throw. See the PR I submitted that fixes this.

Code line

public void SelectAll()
{
    var currentSelectedItems = new List<object>( this.SelectedItems as IEnumerable<object> );

changed to

public void SelectAll()
{
    var currentSelectedItems = new List<object>( this.SelectedItems.Cast<object>() );`
XceedBoucherS commented 3 weeks ago

Hi @darxis, thank you for your feedback.

I can reproduce the issue, but I'm evaluating the need for it. When we have something like : <xctk:CheckComboBox ItemsSource="{Binding MyData}" SelectedItemsOverride="{Binding MySelections}" IsSelectAllActive="True" Width="200" Height="50"/> I can Select all items, but the SelectedItemsOverride property will decide which items to select. So, the SelectedItemsOverride property and the SelectAll option are kind of opposite. We should use one or the other. Unless you have another way of defining the CheckComboBox for this specific case ?

Thank you

darxis commented 3 weeks ago

Hi @XceedBoucherS, thanks for your reply.

What do you mean SelectedItemsOverride and SelectAll are kind of opposite? My use case is that I have a DataGrid with filterable columns. Some of these column values are enum values and I want to filter rows based on these enum column values. For this I am using CheckComboBox for each filterable column and I want the user to be able to select all or unselect all values in the CheckComboBox filtering control by clicking the Select all entry on top of the list as there could be a lot of different enum values, so the user doesn't have to click all entries one by one. When the user clicks the Select all entry in the UI the CheckComboBox should fill the SelectedItemsOverride collection with all the ItemsSource items. Just like in MS Excel :)

PS: I am using something like this:

<xctk:CheckComboBox ItemsSource="{Binding MyItems}" SelectedItemsOverride="{Binding MySelectedItems}" IsSelectAllActive="True" />
darxis commented 3 weeks ago

Also I noticed this bug was not present in WpfToolkit v3.8.2, but I wanted to upgrade to v4.6.1 and I discovered this bug exists in v.4.6.1.

XceedBoucherS commented 3 weeks ago

Thank you @darxis for the explanations. Your fix will be included in the next release, coming soon.