14

I am having a difficult time trying to bind my property which is of type List to my combobox through XAML.

public List<string> MyProperty  { get; set; }

The following XAML binding does not work:

<ComboBox Name="cboDomainNames" ItemsSource="{Binding Path=MyProperty}"/> 

But the following assignment:

cboDomainNames.ItemsSource = MyProperty;

works perfectly. What i am missing here?

heenenee
  • 19,914
  • 1
  • 60
  • 86
ioWint
  • 1,609
  • 3
  • 16
  • 34
  • 1
    Is the DataContext for your ComboBox correct? – Rachel Jul 28 '11 at 05:07
  • Yes the property is in the View ( my app is a MVP- PRISM - WPF) and i have set DataContext= this; And more over the code assignment of ItemSource works! through xaml doesnt! – ioWint Jul 28 '11 at 05:11
  • 1
    I think i nailed it! my DataContext was set! BUT it was set after InitializeComponent(), thought that could be th problem. Then jus realized am binding through xaml! the property gets populated when the view is ready after its loaded (i.e on _presenter.OnViewReady())! Since its not a observable collection Nothing gets added to the combobox! specifying it from my code behind works, coz at that time the data exists in the property!! – ioWint Jul 28 '11 at 05:56
  • @ioWint: Please post an answer which explains what was wrong and how you fixed it and accept that via the checkmark on the left of the answer. – H.B. Jul 28 '11 at 13:14
  • @HB i posted the answer in the comment! i am not sure how to mark it as an answer! – ioWint Jul 28 '11 at 17:44
  • See this answer: https://stackoverflow.com/a/21898127/740639 – Walter Stabosz Nov 14 '19 at 21:56

3 Answers3

10

Posting my comment back to mark the answer.

My DataContext was set, BUT it was set after InitializeComponent(). I thought that could be the problem. Then I realized that as I am binding through xaml, when the view loads, the binding happens to the property which is empty.

The property gets populated when the view is ready after its loaded (i.e on _presenter.OnViewReady()). Since it's not an observable collection nothing gets added to the combobox. Specifying it from my code behind works, because at that time the data exists in the property.

jess
  • 254
  • 3
  • 13
ioWint
  • 1,609
  • 3
  • 16
  • 34
7

Assume you have a List<Foo> called Foos in your window / page. Foo has a property Name. Now you set up the binding in XAML as follows:

<ComboBox ItemsSource="{Binding Path=Foos}"
DisplayMemberPath="Name"
SelectedValuePath="Name"
SelectedValue="{Binding Path=Foo}"
/>

This is based on this SO question. Read this (WPF DataBinding overview) as a good foundation for databinding in WPF.

MHN
  • 103
  • 2
  • 7
Youp Bernoulli
  • 5,303
  • 5
  • 39
  • 59
  • 1
    True, but presently i am interested with a property of List! if code behind assignment of ItemsSource hasnt worked then i wouldnt be more confused! would have gone with a custom entity thinking i always need the DisplayMemberPath! – ioWint Jul 28 '11 at 05:48
  • 4
    Wasn't a solution to binding to a List – JWP Jan 30 '15 at 22:49
0

If you don't specify anything than just the path, the binding assumes as a source the container DataContext. By the way, the useful property is defined on the container (e.g. the window).

You may solve it as follows (in the xaml):

ItemsSource="{Binding Path=MyProperty, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}"
Noam M
  • 3,156
  • 5
  • 26
  • 41
Mario Vernari
  • 6,649
  • 1
  • 32
  • 44
  • i tried this Mario! but hard luck! i couldnt get it populated. view is inheriting from a baseView, so instead of window i put my baseView! it still doesnt get binded! – ioWint Jul 28 '11 at 05:49