3

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

H.B.
  • 166,899
  • 29
  • 327
  • 400
Tvd
  • 4,463
  • 18
  • 79
  • 125
  • How do you know which Unit to use in each case? Where is this information stored? – Nikita B Aug 27 '13 at 11:09
  • @NikitaBrizhak, you see the datagrid1CollectionData() that method is called on Window_Loaded event, and header value of each row is added. I know the series of header and thus know which Unit items will go under which row's combo box. – Tvd Aug 27 '13 at 11:14

1 Answers1

2

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.

vitaliy zadorozhnyy
  • 1,226
  • 10
  • 12
  • How do I add the static resource in my UserControl ? I can add , but don't have access to . How do I add the class converter static resource. BTW, in combobox, you have written "DependedValues...." & the class name is "DynamicValues...", is this a mistake or I am not getting something. – Tvd Aug 27 '13 at 11:47
  • 1
    Please look to last edit of original post how to add converter. – vitaliy zadorozhnyy Aug 27 '13 at 12:27
  • This works. How can I make the 1st item selected for the ones that are added ? And for the ones that are empty, can it be disabled or so, so the drop down of empty list doesn't show up. – Tvd Aug 27 '13 at 12:50
  • 1st Row, got the value of Unit, but 2nd Row didn't get it ? The value for P_Unit of 2nd row was null. ??? – Tvd Aug 27 '13 at 12:55
  • Actually, its working properly, but if I open the empty Unit drop box and click anywhere in the empty list, it gets disturbed. Screen goes up and down and that deleted the lastly selected row's Unit. When I didn't open the empty unit box's, things got saved perfectly well. – Tvd Aug 27 '13 at 13:01