MultiSelectTreeView icon indicating copy to clipboard operation
MultiSelectTreeView copied to clipboard

Binding SelectedItems crash

Open CBenghi opened this issue 4 years ago • 2 comments

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 avatar Dec 10 '21 15:12 CBenghi

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: https://github.com/ygoe/MultiSelectTreeView/blob/8dadce5dfa1fce0216c906921f0edf9551dbc457/MultiSelectTreeView/Controls/MultiSelectTreeView.cs#L89-L91

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.

CBenghi avatar Dec 10 '21 17:12 CBenghi

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.

ygoe avatar Dec 11 '21 22:12 ygoe