0

My little project looks now quite different. Now I have ObservableCollection OC1 with data which I want to bind with whole DatagridTextBoxColumns (via Binding Path) and ObservableCollection OC2 where I store cases for DatagridComboboxColumn (as ItemsSourceBinding). SelectedItem property in DatagridComboboxColumn is one value from OC1 (and it is one of OC2 cases ofc). Binding on DatagridTextBoxColumns is ok.

XAML:

<DataGrid x:Name="DGoc1" x:Uid="DGoc1" AutoGenerateColumns="False"
          AlternationCount="2" SelectionMode="Single" Margin="0,5,0,0" 
          HorizontalAlignment="Stretch">
  <DataGrid.Columns>

    <!-- This works fine -->
    <DataGridTextColumn Binding="{Binding Path=id}" Header="ID" 
                        Width="Auto" IsReadOnly="True"/>

    <!-- Dow to bind this properly?? -->
    <DataGridComboBoxColumn ItemsSource="{Binding OC2}" 
                            SelectedItemBinding="{Binding Path=valueFromOc1}"
                            Header="OC2Cases" Width="Auto"/>

C#(Updated):

public class ClasswithSomeData
{
    public int id { get; set; }
    public string valueFromOc1 { get; set; }
}
public partial class DGCBC : Window
{
    public ObservableCollection<string> OC2 { get; set; }
    public ObservableCollection<ClasswithSomeData> OC1 { get; set;}

    private void tabPanel1_Loaded(object sender, RoutedEventArgs e)
    {
        LoadDG();
    }

public void LoadDG()
    {
    OC2 = new ObservableCollection<string>(someCases);

    OC1 = new ObservableCollection<ClasswithSomeData> { };
    OC1.Add(someData1);
    OC1.Add(someData2);
    OC1.Add(someData3);
    DGoc1.ItemsSource = OC1;

How to bind this DatagridComboboxColumn properly? Please help with some example.

werasquez
  • 99
  • 2
  • 10

1 Answers1

1

your observablecollections are declared as fields:

public ObservableCollection<string> OC2;
public ObservableCollection<ClasswithSomeData> OC1;

they should be Properties:

public ObservableCollection<string> OC2 {get;set;}
public ObservableCollection<ClasswithSomeData> {get;set;} 

Do not forget INotifyPropertyChanged! =)

Federico Berasategui
  • 43,562
  • 11
  • 100
  • 154
  • Updated. I will add `INotifyPropertyChanged` when this binding will works. – werasquez Feb 04 '13 at 17:02
  • @werasquez your code doesn't work **precisely** because you did not `INotifyPropertyChanged`!! and you're doing this in the `Loaded` event which is AFTER all bindings have been evaluated, that's why you're not seeing these changes in the UI – Federico Berasategui Feb 04 '13 at 17:08
  • Ok. My mistake. I read a lot of examples with `INotifyPropertyChanged` used but no one is similar as my. I know how it works but I have no idea how to implement it into my code. If you know that, please help. – werasquez Feb 04 '13 at 22:17