0

I'm kind of new to wpf and data binding and I'm stuck on this.

So basically I have a table of EyeColors and EyeColorIds. Eye Color Id is a foreign key of a Person table.

I created a combobox that is bound to the EyeColor table which populate it with the possible eye colors. However when a user edits a person I want the person's eye color to already be selected. How can I do this?

 <ComboBox
     DataContext="{StaticResource tblEyeColorViewSource}"
     Height="23"
    HorizontalAlignment="Left" 
        Margin="95,125,0,0" 
        Name="EColorBox" 
        VerticalAlignment="Top" 
        Width="120" 
        DisplayMemberPath="EyeColor"
        ItemsSource="{Binding}" />

That is my xaml for the combobox. The eye color of the specific person is obtained when the window is constructed.

So before I create my edit window, i take the data from the datagrid and make a person object

then i construct the edit window

  public AddEditForm(PeopleManagerController pmc, Person p)
    {
        controller = pmc;
        InitializeComponent();
        personToAE = p;

        FnameText.DataContext = personToAE;
        LnameText.DataContext = personToAE;
        datePicker1.DataContext = personToAE;
        datePicker1.Text = personToAE.DateOfBirth;
        AddEditButton.Content = "Edit";
    }

Then when the user clicks the edit button it sends the person to edit to the controller class for SQL transaction.

As for the xaml, I'm just creating controls and binding them to the person object properties. I want the eye color property of the person to edit to be the selected value of the combobox.

user1865017
  • 5
  • 1
  • 3

1 Answers1

0

It is a bit difficult when you haven't provided all of the relevant information, so I'm going to have to assume some things.

<ComboBox DataContext="{StaticResource tblEyeColorViewSource}" Height="23"
HorizontalAlignment="Left" Margin="95,125,0,0" Name="EColorBox" VerticalAlignment="Top" 
Width="120" DisplayMemberPath="EyeColor" ItemsSource="{Binding}" SelectedValuePath="Id" 
SelectedValue={Binding CurrentPerson.EyeColourId}" />

Note that the SelectedValuePath property must be set to the exact name of the EyeColourId property in the tblEyeColorViewSource data object, whatever that may be called. This means that when a value is selected, we want to receive the value from this property... it's like the DisplayMemberPath property, but that one specifies which property will be displayed when a value is selected.

Now the SelectedValue property must be set to the object that references the current Person object and the property that we want to set.

I've just noticed that you have set your ComboBox.DataContext to {StaticResource tblEyeColorViewSource} and its ComboBox.ItemsSource to {Binding}. Does that even work? I'm assuming that your tblEyeColorViewSource is a CollectionViewSource, so shouldn't that code be:

DataContext="{Binding Source={StaticResource tblEyeColorViewSource}}" 

Either way, if you set your ComboBox.ItemsSource to {Binding}, then that means that you may have no selected item to bind to the ComboBox.SelectedValue property - this is essential for what you want.

Sheridan
  • 68,826
  • 24
  • 143
  • 183
  • I don't think I can access the person object because its not in the same data context, is there a way I can do that? – user1865017 Aug 12 '13 at 14:46
  • Look online for the `Model, View, View Model (MVVM)` design pattern. The basic idea is that you create a view model for each view (`Window`/`UserControl`). The view model provides *all* of the data that each view requires. Each view model is set as the `DataContext` for its relating view. This methodology really suits WPF perfectly and I recommend that you investigate it... it makes WPF so much easier. – Sheridan Aug 12 '13 at 14:54