I need to bind a ComboBox's Selected Item according to a value of a field in the data context which it's parent container receives .
the container is a grid which receives it's datacontext from an item in an itemcontrol when it's clicked
private void Button_Click(object sender , RoutedEventArgs e)
{
GridEmployee.DataContext = ((Button)sender).DataContext;
}
*the Button got it's itemsource from a list of Employee's Bounded to the itemControl
the grid holds some controls amongst them a combobox which i initalize through an Enum
public Enum Gender
{
Male,Female
};
foreach(string _gender in Enum.GetNames(Gender) )
{
GenderComboBox.Items.Add(_gender);
}
the Employee class has a matching Property Gender
private string gender;
public string Gender
{
get{return gender;}
set
{
gender = value ;
if( PropertyChanged != null )
PropertyChanged(this,new PropertyChangedEventArgs("Gender"));
}
}
the GenderComboBox.SelectedItem is bounded to the value of the Gender Property for the bounded object Employee
<ComboBox x:Name="GenderComboBox" SelectedItem="{Binding Gender , Mode=TwoWay}" />
the problem here of course that the item does not get selected ..
I tought may be its becuase the items in the combobox are strings and I try to bound them according to a custom converter which just take the Enum Value and returns the .ToString() of it .
but I was not able to check this becuase that threw an An XamlParseException in form's contractor .
which I did not fully understand why it happend ,may be becuase it did not have a value to convert when I form loads.
so to conclude how do I bind a Property from My Employee Class to a combobox with the string representation of the Property's Value?