Search code examples
c#wpfxamldata-bindingwpfdatagrid

how to bind custom class collections to wpf datagrid combobox (multiselected)


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.


Solution

  • 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>