Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NullReferenceException when SelectAll for collection of value types #1787

Open
darxis opened this issue Oct 26, 2024 · 6 comments
Open

NullReferenceException when SelectAll for collection of value types #1787

darxis opened this issue Oct 26, 2024 · 6 comments

Comments

@darxis
Copy link

darxis commented Oct 26, 2024

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.

darxis added a commit to darxis/wpftoolkit that referenced this issue Oct 26, 2024
@XceedBoucherS
Copy link
Collaborator

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
Copy link
Author

darxis commented Oct 28, 2024

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
Copy link
Collaborator

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
Copy link
Author

darxis commented Oct 29, 2024

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
Copy link
Author

darxis commented Oct 29, 2024

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
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants