草庐IT

c# - WPF 数据网格 : DataGridComboxBox ItemsSource Binding to a Collection of Collections

coder 2024-05-29 原文

情况:

我在 XAML 中创建了一个 DataGrid,并且 ItemsSource 绑定(bind)到包含属性的特定类的 ObservableCollection。然后在 C# 中,我创建了一个 DataGridTextColumn 和一个 DataGridComboBoxColumn,并将它们绑定(bind)到 ObservableCollection 中对象的属性。我可以将 DataGridComboBoxColumn 绑定(bind)到一个简单的 Collection,但我想要做的是将它绑定(bind)到一个字符串集合的集合,这样对于每一行,DataGrid 中的 ComboBox 都有一个不同的字符串集合。我没有这样做......

问题:

如何绑定(bind) DataGridCombBoxColumn 以便我可以为此类列的每一行拥有不同的字符串集合?

代码示例:

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_
};
//...

如有任何帮助,我们将不胜感激!

注意事项:

好的,所以在阅读前两个答案后,我注意到了一些事情。我的绑定(bind)真的不对啊!现在,我想做的事情与 AndyG 提出的类似:

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 绑定(bind)到 Data?

最佳答案

首先,这应该很容易……其次,为什么要在 C# 中构建(和绑定(bind))列?嗯。

XAML(我使用的是常规网格,因为我很懒):

<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 grid with dynamic combo box choices http://www.singingeels.com/Articles/Articles/UserImage.aspx?ImageID=b1e3f880-c278-4d2b-bcc2-8ad390591200

关于c# - WPF 数据网格 : DataGridComboxBox ItemsSource Binding to a Collection of Collections,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1633800/

有关c# - WPF 数据网格 : DataGridComboxBox ItemsSource Binding to a Collection of Collections的更多相关文章

随机推荐