1

I am trying to populate the wpf combobox with list. I have two problems with it.

  1. It doesn't populate anything
  2. I am using Data Annotation for the validation. It doesn't set "Required" Error message in error display area.

Here is my XAML for the combobox:

<Label Target="{Binding ElementName=State}" Grid.Row="10" Grid.Column="0">State:</Label>
        <ComboBox x:Name="State" Margin="10,0,0,10" Grid.Column="1" Grid.Row="10" ItemsSource="{Binding Path=States, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnDataErrors=True}"  Validation.Error="Validation_Error" SelectedValue="{Binding Path=FamilyMember.State}"/>   
        <TextBlock Grid.Column="2" Grid.Row="10" Style="{StaticResource TextBlockStyle}" Text="{Binding (Validation.Errors)[0].ErrorContent, ElementName=State}" Margin="10,0,0,10"/>

Here is my partial viewmodel where I am declaring and populating my States object.

Property in ViewModel

public ObservableCollection<string> States;

Constructor:

States = new ObservableCollection<string>();
            States.Add("One");
            States.Add("Two");
            States.Add("Three");
            States.Add("Four");
            States.Add("Five");

Here is the proof from my Debug that I am getting states correctly to a view. enter image description here

And another problem is that my data annotation error is not working Here is my partial Model: enter image description here

It is working for other fields without any problems as seen below:

enter image description here

2
  • 1
    Property in ViewModel public ObservableCollection<string> States; -- That's not a property. It's a field. You can't bind to it. You can only bind to properties. You need to make a it a property by giving it { get; set; }. Second, your "proof" that you're "getting states correctly to a view" doesn't make any sense to me; what do you mean when you say "view"? Commented Jan 6, 2017 at 18:57
  • As a note, setting UpdateSourceTrigger=PropertyChanged and Mode=TwoWay on the ItemsSource Binding is pointless, as the source is never updated by the Binding. Commented Jan 6, 2017 at 19:31

2 Answers 2

9

Change this field:

public ObservableCollection<string> States;

to a property:

public ObservableCollection<string> States {get; set;}

Binding does not work on fields even if they are public.

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

5 Comments

Thanks CodingYoshi, Getter and setter for the States fixed the populating issue. That was really a quick fix. But second problem still persists. My Data Annotation validation doesn't trigger if it is not selected OR even initially when my view is loaded as seen above in my screenshot
Try SelectedItem not SelectedValue
I found the problem.. why it was not triggering.. because I was missing Mode, NotifyOnValidationError and ValidatesOnDataErrors.. I till used the SelectedItem as you specified. Thanks CodingYoshi. I am marking your original answer as solution to a problem
But they are both there: NotifyOnValidationError=True, ValidatesOnDataErrors=True. Anyways, glad you fixed it.
@AndyJohnson That's incorrect. Those voodoo attributes had nothing to do with it. Look them up. All of them are about the Binding updating from the control property to the bound viewmodel property, but ItemsSource cannot ever update the viewmodel property. Never. It's one way, only.
-1

Change the States as full property in your view model and add the display member path in your combo box for your xaml file.

View Model:

public ObservableCollection<string> _state = new ObservableCollection<string>();
public ObservableCollection<string> States 
{
 get{return _state;}
set {_state = value; OnPropertyChange("States");}
}

If you need more clarification refer this page: https://www.codeproject.com/Articles/301678/Step-by-Step-WPF-Data-Binding-with-Comboboxes

1 Comment

DisplayMemberPath makes no sense when binding to a collection of strings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.