GridComboxBox的ItemsSource绑定到的集合集

GridComboxBox的ItemsSource绑定到的集合集

本文介绍了WPF的DataGrid:DataGridComboxBox的ItemsSource绑定到的集合集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建XAML中一个DataGrid和的ItemsSource被绑定到包含属性某一类的一个ObservableCollection。然后在C#中,我创建了一个DataGridTextColumn和DataGridComboBoxColumn和这些绑定到的ObservableCollection内的对象的属性。我可以DataGridComboBoxColumn绑定到一个简单的集合,但我想要做的就是将其绑定到字符串集合的集合,使每一行的组合框里面的DataGrid中有串的不同集合。我已经没有这样做...

I've created a DataGrid in XAML and the ItemsSource is binded to an ObservableCollection of a certain class that contains properties. Then in C#, I create a DataGridTextColumn and a DataGridComboBoxColumn and binded these to the properties of the objects inside the ObservableCollection. I can bind the DataGridComboBoxColumn to a simple Collection but what I want to do is bind it to a collection of collections of strings so that for each row the ComboBox inside the DataGrid has a different collection of string. I have failed to do so...

如何绑定DataGridCombBoxColumn,这样我可以有一个字符串不同的集合这种类型的列?

How can I bind the DataGridCombBoxColumn so that I can have a different collection of strings for each row of this type of column?

XAML:

<Window>
  <!-- ... -->
  WPFToolkit:DataGrid
           x:Name="DG_Operations"
           Margin="10,5,10,5"
           Height="100"
           HorizontalAlignment="Stretch"
           FontWeight="Normal"
           ItemsSource="{Binding Path=OperationsStats}"
           AlternatingRowBackground="{DynamicResource SpecialColor}"
           HorizontalScrollBarVisibility="Auto"
           VerticalScrollBarVisibility="Visible"
           SelectionMode="Extended"
           CanUserAddRows="False"
           CanUserDeleteRows="False"
           CanUserResizeRows="True"
           CanUserSortColumns="True"
           AutoGenerateColumns="False"
           IsReadOnly="False"
           IsEnabled="True"
           BorderThickness="1,1,1,1"
           VerticalAlignment="Stretch"/>
  <!-- ... -->
</Window>

C#:

public class DataModelStatsOperations
{
   public ObservableCollection<IStatsOperation> OperationsStats { get; set; }
}

public interface IStatsOperation
{
   string Operation { get; set; }
   Collection<string> Data{ get; set; }
}

public class StatsOperation : IStatsOperation
{
    public StatsOperation(string operation, Collection<string> data)
    {
        Operation = operation;
        Data = data;
    }
    public string Operation { get; set; }
    public Collection<string> Data{ get; set; }
}

private ObservableCollection<IStatsOperation> dataOperations_ =
        new ObservableCollection<IStatsOperation>();

//...
 Binding items = new Binding();
 PropertyPath path = new PropertyPath("Operation");
 items.Path = path;
 DG_Operations.Columns.Add(new DataGridTextColumn()
 {
     Header = "Operations",
     Width = 133,
     Binding = items
  });
  DG_Operations.Columns.Add(new DataGridComboBoxColumn()
  {
     Header = "Data",
     Width = 190,
     ItemsSource = /*???*/,
     SelectedValueBinding = new Binding("Data"),
     TextBinding = new Binding("Data")
  });
dataOperations_.Add(new StatsOperation(CB_Operation.SelectedItem.ToString(),
                                                           dataCollection));
DG_Operations.DataContext = new DataModelStatsOperations
{
    OperationsStats = dataOperations_
};
//...

任何帮助将大大AP preciated!

Any help would be greatly appreciated!

好了,看完两个第一的答案后,我发现了一些。我的结合真的是不对的!现在,我想要做的是类似于AndyG建议的内容:

Okay, so after reading the two first answers I noticed something. My binding is really not right! Now, what I want to do is something similar to what AndyG proposed:

DG_Operations.Columns.Add(new DataGridComboBoxColumn()
{
    Header = "Data",
    Width = 190,
    ItemsSource = new Binding("Data"), //notice this here does not work (have a look at the following error)
    SelectedValueBinding = new Binding("Operation"),
    TextBinding = new Binding("Operation")
});

错误:无法隐式转换类型'System.Windows.Data.Binding'到'System.Collections.IEnumerable'

如何能的ItemsSource绑定到数据?

How can the ItemsSource be bound to Data?

推荐答案

首先,这应该是在C#中容易。其次,你为什么要建设(和有约束力的)列?伊克。

Firstly, this should be easy... secondly, why are you building (and binding) columns in C#? Eek.

XAML (我使用规则的网格因为我懒):

XAML (I'm using a regular grid because I'm lazy):

<ListView Name="MyListView">
    <ListView.View>
        <GridView>

            <GridView.Columns>

                <GridViewColumn DisplayMemberBinding="{Binding Operation}" />

                <GridViewColumn>
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox ItemsSource="{Binding Choices}" />
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>

            </GridView.Columns>

        </GridView>
    </ListView.View>
</ListView>

C#

void Window1_Loaded(object sender, RoutedEventArgs e)
{
    var dahList = new List<StatsOperation>();

    dahList.Add(new StatsOperation
    {
        Operation = "Op A",
        Choices = new string[] { "One", "Two", "Three" },
    });

    dahList.Add(new StatsOperation
    {
        Operation = "Op B",
        Choices = new string[] { "4", "5", "6" },
    });

    this.MyListView.ItemsSource = dahList;
}

结果:

这篇关于WPF的DataGrid:DataGridComboxBox的ItemsSource绑定到的集合集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 07:43