-1

I have such an ObservableCollection

private ObservableCollection<IRequest> RequestSet = new ObservableCollection<IRequest>(); 

and here my init method

internal void FormLoaded() { List<IRequest> requestSet = GenerateRequestSet(RequestType); RequestSet = new ObservableCollection<IRequest>(requestSet); } 

Here how I bind this property in XAML

<TreeView Name="Tv_request" Grid.Row="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ItemsSource="{Binding Path=RequestSet}"> 

and I see that UI doesn't get this update.

What am I doing wrong?

1
  • Did you try making it a property? Commented Oct 3, 2020 at 17:30

2 Answers 2

2

From the documentation on binding sources:

You can bind to public properties, sub-properties, as well as indexers, of any common language runtime (CLR) object.

So you have to make RequestSet a public property, e.g.:

public ObservableCollection<IRequest> RequestSet { get; } 

You do not need to assign it directly, if you overwrite its value in the constructor anyway.

Sign up to request clarification or add additional context in comments.

Comments

1

If you want to be able to use a binding the "source" must be a property and it must be public (yours is currently a private field).

If you want to be able to update the property at runtime and the changes to be reflected in the UI then you must also implement INotifyPropertyChange for the class it's implementing the property as well as the property must publish it's changes.

https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.inotifypropertychanged?view=netcore-3.1

Btw, when looking at your code then you dont need the observable collection, a simple IEnumerable will be sufficient. Observable collections are only required when you want to add items to it at runtime. Also, if this is what you want then I suggest to make the property a readonly property as setting it again during runtime will break the binding unless INotifyPropertyChanged is implemented

2 Comments

You are right I need ObservableCollection because in some cases I need to update this list. And if I need to assign new value for current collection I need to make it with INotifyPropertyChanged , actually I thought that ObservableCollection make it under the hood... my mistake.
@AlekseyTimoshchenko The observableCollection does not need to be INotifyPropertyChanged 'enhanced' - a plain readonly property is sufficient then, as long as you only call methods/properties to that instance (like add/remove/count/clear). If the the property is not readonly and you do update the property with a new collection then that action will break the binding since the binding is connected with the instance and not with the actual property. If you are setting the property with a new value (often the case with int's and strings) then you do need INotifyPropertyChanged

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.