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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.