Add DataGrid checkboxcolumn with different values

2019-07-01 15:14发布

I got a DataGrid that is bound to an object PlacementData (PD). PD has a property "P_Unit".

 public class PlacementData 
 {
    public bool PIsChecked { get; set; }
    public string PlacementHeader { get; set; }
    public string P_NumberOfCases { get; set; }
    public int P_Value1 { get; set; }
    public int P_Value2 { get; set; }
    public int P_Value3 { get; set; }
    public int P_Value4 { get; set; }
    public int P_Value5 { get; set; }
    public string P_Unit { get; set; }
}

In my DataGrid I added a Combobox in DataTemplateColumn.

<DataGridTemplateColumn x:Name="UnitColumn1" Header="Unit" MinWidth="80" >
     <DataGridTemplateColumn.CellTemplate>
           <DataTemplate>
                  <ComboBox Text="{Binding P_Unit}">
                         <ComboBoxItem Content="kg/m3" IsSelected="True"/>
                         <ComboBoxItem Content="gm/cm3"/>
                  </ComboBox>
            </DataTemplate>
      </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

On start of the window, I set the itemsource with 4 rows with headers added.

    private List<PlacementData> datagrid1CollectionData()
    {
        List<PlacementData> authors = new List<PlacementData>();

        authors.Add(new PlacementData()
        {
            PlacementHeader = "Based On Injection Rate",

        });
        authors.Add(new PlacementData()
        {
            PlacementHeader = "Based On Viscosity"
        });
        authors.Add(new PlacementData()
        {
            PlacementHeader = "Based On Sheer Thinning"
        });
        authors.Add(new PlacementData()
        {
            PlacementHeader = "k"
        });


        return authors;
    }

    dataGrid1.ItemsSource = datagrid1CollectionData();

My each row need different values for Unit combo box. For eg., 1 row needs "kg, gm", 2nd needs "meter, cm, feet", 3rd needs "ltr, ml, ton", & 4th needs it to be blank.

How do I set these values ? I think on each row creation, I can create a List and assign that as itemsource to the checkbox. But how is this possible in the above code. Checkbox Itemsource for each row of checkbox ???

1条回答
姐就是有狂的资本
2楼-- · 2019-07-01 15:55

I would recommend to use EditCellTemplate but it is up to you and task requirements. In the combobox in the DataTemplate use custom IValueConverter (I have used PlacementHeader as dependand property, you can use what actually needed or PlacementData itself):

 <ComboBox SelectedValue ="{Binding P_Unit}" ItemsSource="{Binding PlacementHeader, Converter={StaticResource DependedValuesConverter}}">
                  </ComboBox>

and some sample of converter just like idea:

    public class DynamicValuesConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value != null)
            switch (value.ToString())
            {
                case "Based On Injection Rate":
                    return new[] { "kg/m3", "gm/cm3" };
                case "Based On Viscosity":
                    return new[] { "some other..." };
            }
        return new string[0];
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

To implement multi selection on combobox you can use some open source CheckComboBox.

EDIT According to your comment: you can add converter anywhere where it is visible to data template I have added directly to datatemplate just to demonstrate:

                    <DataGridTemplateColumn.CellEditingTemplate>
                    <DataTemplate>
                        <DataTemplate.Resources>
                            <local:DynamicValuesConverter x:Key="DependedValuesConverter" />
                        </DataTemplate.Resources>
                        <ComboBox SelectedValue="{Binding P_Unit}" ItemsSource="{Binding PlacementHeader, Converter={StaticResource DependedValuesConverter}}"></ComboBox>
                    </DataTemplate>
                </DataGridTemplateColumn.CellEditingTemplate>

"local" has to point to your DynamicValuesConverter namesapce.

查看更多
登录 后发表回答