2

I created a DataGrid and added a DataGridComboBoxColumn programmatically.

public partial class MainWindow : Window
{

    private DataGridComboBoxColumn weightColumnChar = new DataGridComboBoxColumn();
    ObservableCollection<int> mComboBoxValues;
    public ObservableCollection<int> ComboBoxValues
    {
        get { return this.mComboBoxValues; }
        set { this.mComboBoxValues = value; }
    }//end property
    public MainWindow()
    {
        InitializeComponent();
        mComboBoxValues = new ObservableCollection<int>() {-1, 0, 1 };
    }//end constructor
    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        weightColumnChar.Header = "Weight";
        dataGrid_Char.Columns.Add(weightColumnChar);

        weightColumnChar.ItemsSource = ComboBoxValues;
        Binding binding = new Binding();
        binding.Path = new PropertyPath(ComboBoxValues[1]);
        weightColumnChar.SelectedItemBinding = binding;
    }//end method
    private void dataGrid_Char_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {

    }//end method
    //Opens ComboBox on first click
    private void dataGrid_Char_GotFocus(object sender, RoutedEventArgs e)
    {
        if (e.OriginalSource.GetType() == typeof(DataGridCell))
        {
            DataGrid grd = (DataGrid)sender;
            grd.BeginEdit(e);
        }//end if
    }//end method
}//end class

I added an ItemsSource to it and retrieve the values from an ObservableCollection.

The values from the collection are shown in runtime.

My problem is that if I select a value from the ComboBox this vlaue isn't selected and displayed afterwards. What am I doing wrong?

And I also want to select a default value. How does that work?

Please explain programmatically and not in XAML!

Would be great if anybody could help me.

Thanks!!!

STiLeTT
  • 1,023
  • 10
  • 23
  • There is a reason to not use XAML ? – Franck Charlier Sep 19 '12 at 09:31
  • My Problem is, that the changes aren't shown in the comboBoxColumn. So I think it has something to do with the right binding. I can't do this in XAML because I added the dataGridComboBoxColumn programmatically, so I don't have it in XAML. – Chagingelle Sep 19 '12 at 11:10

1 Answers1

0

First of all you haven't shown what your underlying collection is that is bound to the DataGrid.

You'll need something like DataGrid.ItemsSource = new ObservableCollection<MyClass>(); (it has to be a collection that supports change notification, thus I chose ObservableCollection).

Secondly you are binding the ComboBox.SelectedItemBinding to the ComboBox.ItemsSource which is nonsense. ComboBox.ItemsSource is the collection of values you can choose from, ComboBox.SelectedItemBinding (I would actually use ComboBox.SelectedValueBinding) is the "path" to the underlying data source that contains/reprsents the final value (eg. MyClass.IntProperty). So you select a value from a collection and assign that to some data item (you cannot assign it back to the collection where you select from).

PS! In case you later use something that resembles a KeyValuePair (eg. Id; Name) as your ComboBox.ItemsSource, then you set ComboBox.SelectedValuePath = Id; ComboBox.DisplayMemberPath = Name;. MyClass.IntProperty would contain the Id value in such a case.

Marko
  • 2,266
  • 4
  • 27
  • 48