eran otzap
eran otzap

Reputation: 12533

Binding ComboBox SelectedItem to DataContext Value

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?

Upvotes: 2

Views: 3889

Answers (2)

Vinit Sankhe
Vinit Sankhe

Reputation: 19895

Works nicely in my case....

XAML

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:tk="http://schemas.microsoft.com/wpf/2008/toolkit"
    Title="GenderSelection" Height="100" Width="300" x:Name="MyWindow">
  <Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <TextBlock FontSize="40"
               Text="{Binding MyGender, ElementName=MyWindow, Mode=TwoWay}"/>
    <ComboBox Grid.Row="1"
              ItemsSource="{Binding Genders, ElementName=MyWindow}"
              SelectedItem="{Binding MyGender, ElementName=MyWindow, Mode=TwoWay}">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding}"/>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>
  </Grid>    
</Window>

Code Behind

public enum Gender
{
    Male,
    Female
}

/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window, INotifyPropertyChanged
{
    private string myGender = Gender.Male.ToString();
    public string MyGender
    {
        get
        {
            return myGender;
        }

        set
        {
            myGender = value;

            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs("MyGender"));
            }
        }
    }

    public string[] Genders
    {
        get
        {
            return Enum.GetNames(typeof(Gender));
        }
    }

    public Window1()
    {
        InitializeComponent();
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    #endregion
  }

Let me know if this guides you in correct direction...

Upvotes: 2

Fischermaen
Fischermaen

Reputation: 12468

Just change the initialization of your ComboBox to

foreach(string _gender in Enum.GetNames(Gender) ) 
{ 
   GenderComboBox.Items.Add(_gender.ToString()); 
} 

That should work, because your Gender property of the Employees class returns a string.

Upvotes: 0

Related Questions