Skip to content
This repository has been archived by the owner on May 7, 2022. It is now read-only.

Binding SelectedItems crash #56

Open
CBenghi opened this issue Dec 10, 2021 · 2 comments
Open

Binding SelectedItems crash #56

CBenghi opened this issue Dec 10, 2021 · 2 comments

Comments

@CBenghi
Copy link

CBenghi commented Dec 10, 2021

Hello,

maybe I'm doing something very silly, but if I try to bind the SelectedItems property of the control I get exceptions as the dependencyproperty seems to be null, even if I initialised it.

Is there something big I'm missing?

Thanks,
Claudio

@CBenghi
Copy link
Author

CBenghi commented Dec 10, 2021

I've looked at the code and I think the problem lies with the initialization of the SelectedItems property.

It is initialized at the following lines in the constuctor:

public MultiSelectTreeView()
{
SelectedItems = new ObservableCollection<object>();

whereas it should be initialized in the metadata of the DependencyProperty with something like the following:

public static DependencyProperty SelectedItemsProperty = DependencyProperty.Register(
	"SelectedItems",
	typeof(IList),
	typeof(MultiSelectTreeView),
	new FrameworkPropertyMetadata(
		new ObservableCollection<object>(), 
		FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, 
		OnSelectedItemsPropertyChanged)
	);

I'm happy to post a PR if this solution works for you.

@ygoe
Copy link
Owner

ygoe commented Dec 11, 2021

I think I've never done that. My approach was to bind each item's IsSelected property (this is prone to missed notifications while items are virtualised away) or do the selection stuff in the view code (.xaml.cs) instead. I'm not using this code anymore and I can't offer much support for it now.

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

No branches or pull requests

2 participants