添加具有不同行的DataGrid复选框列 [英] Add DataGrid checkboxcolumn with different values in different rows

查看:143
本文介绍了添加具有不同行的DataGrid复选框列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DataGrid绑定到一个对象PlacementData(PD)。 PD具有属性P_Unit。

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; }
}



在我的DataGrid中,我在DataTemplateColumn中添加了一个Combobox。

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>

在窗口开始时,我设置了itemsource为4行,添加了标题。

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();

我的每一行需要不同的值组合框。例如,1行需要kg,gm,第二需要米,cm,英尺,第三需要ltr,ml,ton 4th需要它为空白。

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.

如何设置这些值?我认为在每行创建,我可以创建一个列表,并将其作为itemsource分配到复选框。但是如何在上面的代码中这是可能的。复选框Items的每一行复选框

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

推荐答案

我建议使用EditCellTemplate,但它是由你和任务要求。
在DataTemplate的组合框中使用自定义IValueConverter(我已经使用PlacementHeader作为依赖属性,你可以使用实际需要的或PlacementData本身):

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>

和一些转换器示例:

    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();
    }
}

要在combobox上实现多选,来源 CheckComboBox

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

EDIT
根据您的意见:您可以在数据模板可见的任何地方添加转换器我已经直接添加到datatemplate只是为了演示:

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必须指向您的DynamicValuesConverter namesapce。

"local" has to point to your DynamicValuesConverter namesapce.

这篇关于添加具有不同行的DataGrid复选框列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆