0

Please help me figure out how to bind custom class collection to datagrid combobox. My custom class is

class Test: INotifyPropertyChanged
{
    public String Name { get; set; }
    public UserAvailableValue SelectedAvailableValue { get; set; }
    public ObservableCollection<UserAvailableValue> AvailableValues { get;  set; }
    public ObservableCollection<String> DefaultValues { get;  set; }
    public String SelectedValue { get; set; }

    public event PropertyChangedEventHandler PropertyChanged;
    private void RaisePropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

public class UserAvailableValue 
{
    public Object Value { get; set; }
    public Object Label { get; set; }
}

From code behind, i am setting DataGrid Datacontext i.g.

   ObservableCollection<Test> UIParams = new ObservableCollection<Test>();
   // code to fill UIParams collection
   dgReportparameters.DataContext = UIParams; 

   //XAML Code
   <DataGrid Name="dgReportparameters" ItemsSource="{Binding}"
               AutoGenerateColumns="False">
     <DataGrid.Columns>
     <DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
     <DataGridComboBoxColumn Header="Available Values" SelectedItemBinding=
      "{Binding SelectedAvailableValue, UpdateSourceTrigger=PropertyChanged}" 
       DisplayMemberPath="Label">
        <DataGridComboBoxColumn.ElementStyle>
        <Style TargetType="ComboBox">
        <Setter Property="ItemsSource" Value="{Binding Path=AvailableValues, 
         RelativeSource={RelativeSource AncestorType=Window}}" />
         </Style>
        </DataGridComboBoxColumn.ElementStyle>
      </DataGridComboBoxColumn>
   <DataGridTextColumn Header="Default Values" Binding="{Binding SelectedValue}"/>
   <DataGridCheckBoxColumn Header="Nullable" Binding="{Binding IsNullable}"/>
 </DataGrid.Columns>
</DataGrid>

Except DataGridComboBoxColumn other columns are showing correct values.DataGridComboBoxColumn is showing blank column. UIParams collection has multiple parameters while each parameter has name and Available values and one default value. I want to show paramters in datagrid and let user select one/multiple values from Available column combobox. Each parameter has its own set of available values. Most of the example i found have Common collection in dropdown but in my case each row of datagrid has different available values.

Please help me to have combobox in datagrid.

Thanks in advance.

107
  • 552
  • 3
  • 26

2 Answers2

0

Your Combobox Style is wrong. The ItemsSource is set to Window whereas from your Model, you don't need to set the AncestorType.

Just {Binding} is enough. Actually, you don't need the ItemsSource Setter as you are just defining the style of the Combobox. You need it only if you are modifying the ItemsSource to something else. In your case, its not.

Edit: Take look on this example: Binding ItemsSource of a ComboBoxColumn in WPF DataGrid I don't understand your requirement of having AvailableValues collection in each Data Item which you have have it in Top Level like below.

public class MyClass
{
public List<Test> Items{get;set;}
public List<AvailableValue> AvailableValues { get;set;}
}

I also notices that you have implemented INotifyPropertyChanged interface and not raising the change event on the each property set.

I would suggest you to learn some basic of WPF and INotifyPropertyChanged interface before you start working on them.

Community
  • 1
  • 1
  • i did not get your point. Even after modifying ConboBox Style to combobox is blank. – 107 Jan 05 '15 at 21:22
  • to make question short i modified code on stackoverflow text editor and made each property simple getter and setter without raising onchange event. – 107 Jan 05 '15 at 22:34
0

This helped me.

    <DataGridComboBoxColumn Header="Available Values" SelectedItemBinding=
                 "{Binding SelectedAvailableValue, UpdateSourceTrigger=PropertyChanged}"                     
                 DisplayMemberPath="Label">
            <DataGridComboBoxColumn.EditingElementStyle>
               <Style TargetType="ComboBox">
                  <Setter Property="ItemsSource" Value="{Binding Path=AvailableValues}" />
               </Style>
            </DataGridComboBoxColumn.EditingElementStyle>
  </DataGridComboBoxColumn> 
107
  • 552
  • 3
  • 26