How to Link an ItemSource to Private Property

How to link WPF a ItemsSource to private property?

 <ComboBox x:Name="xxx" ItemsSource="{Binding Items, Mode=OneWay}" DisplayMemberPath="ItemName"/> 
 public partial class ItemBuySellAddEdit : BasePage { private List<Item> Items { get; set; } } 

The list of items will be filled during the loading of the form.

+8
c # data-binding wpf xaml
source share
4 answers

DataBinding in WPF only works with public properties.

MSDN :

The properties that you use as properties of the binding source for the binding must be public properties of your class. The explicit interface of the property cannot be accessed for binding purposes and cannot be protected, private, internal or virtual properties that do not have a base implementation

+20
source share

If you really really wanted to do this, you would need to provide a custom type descriptor by executing ICustomTypeDescriptor - which provides an additional property through a custom PropertyDescriptor along regular regular public properties. You can implement this interface for the type itself or through TypeDescriptionProvider ; the latter is preferable since it works in more scenarios (for example, empty lists, without requiring also the provision of a custom list with the implementation of ITypedList ). This is a lot of work, but really not worth it, except in extreme cases. But it can be done.

+3
source share

This is not possible if you want, you could use internal instead.

... and use ObservableCollection<T> and don't forget to set the DataContext of the view.

+2
source share

You will need a Relative Source binding, now your DataContext binding of your ItemBuySellAddEdit (FrameworkElement) Atleast is my Impression because you use partial . If it's a ViewModel, check the output window and see if you have any binding errors.

 <ComboBox x:Name="xxx" ItemsSource="{Binding Items, RelativeSource={RelativeSource AncestorType={x:Type ItemBuySellAddEdit}}, Mode=OneWay}" DisplayMemberPath="ItemName"/> 

But the answer from Stefan Bauer still applies.

Also consider the response from WaltiD if you want new items in this list to be displayed automatically.

+1
source share

All Articles