如何将我的 Windows 社区模板数据网格的数据项行上的集合用作同一行上 ComboBox 列的 Itemsource?

How can I use a collection on the data item row of my Windows Community Template datagrid as the Itemsource for a ComboBox column on the same row?

如何将 Combobox 列的 ItemsSource 绑定到作为同一行上 属性 的子 属性 的集合?换句话说,DataGrid 绑定到 class 的项目集合,其中包含 Property1 和 Property2。因此 DataGrid 有两列,Property1 和 Property2。 Property1 有一个 sub-属性,它是一个 Observable Collection。 Property2 的列是一个 Combobox 列,应该使用 Property1 的 Observable Collection 作为其 ItemsSource。

我正在通过 EFCore 加载网格的源数据。我已经确保使用“.Include”来确保 Property1 的 Observable Collection 被加载到内存中。我想知道问题是否是 UI 在从数据库加载时没有意识到 Property1 的 Observable 集合已更新?如果这是问题所在,我该如何纠正?

我需要此集合作为 Property2 列的 ItemsSource,如下所示。我试过使用相对源来获取网格的数据上下文,但它不起作用。

<wct:DataGridComboBoxColumn  Binding="{Binding Property2, Mode=TwoWay}"                                                
                             ItemsSource="{Binding Path=DataContext.Property1.MyCollection, 
                             RelativeSource={RelativeSource TemplatedParent}, 
                             Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}">

我也试过这个:

ItemsSource="{Binding ElementName=grid, 
              Path=DataContext.Property1.MyCollection}"

谢谢。

导出官方processing method,更好的方法是在后面的代码中设置DataGridComboBoxColumn。给 DataGridComboBoxColumn 一个标签名称,然后找到带有如下标签的列,然后设置 DataGridComboBoxColumn ItemsSource

MyCollection = new List<string> { "DD", "FF", "CC" };           
var comboBoxColumn = MyDataGrid.Columns.FirstOrDefault(x => x.Tag.Equals("Link")) as DataGridComboBoxColumn;
if (comboBoxColumn != null)
{
    comboBoxColumn.ItemsSource = MyCollection;
}
MyDataGrid.ItemsSource = items;

Xaml代码

<controls:DataGridComboBoxColumn
    Width="*"
    Binding="{Binding p2}"
    Header="Link"   
    Tag="Link"
    />

DataGridComboBoxColumn ItemsSource 属性 无法直接访问数据源子 属性,因此我们需要为使用的 Page class 创建一个列表 属性存储 Property1.MyCollection。如果您已经为页面 class 设置了 MyCollection 属性,您还可以使用 x:bind 访问,如下所示。

<controls:DataGridComboBoxColumn 
    Width="*"
    Binding="{Binding p2}"
    Header="Link"   
    ItemsSource="{x:Bind MyCollection,Mode=OneWay}"
    Tag="Link"
    />

更新

如果 Property1.MyCollection 不是常量列表,您可以尝试使用 DataGridTemplateColumn 自定义列单元格并绑定数据源,如下所示

<controls:DataGridTemplateColumn>
    <controls:DataGridTemplateColumn.CellEditingTemplate>
        <DataTemplate>
            <ComboBox
                HorizontalAlignment="Stretch"
                VerticalAlignment="Center"
                ItemsSource="{Binding p1.Mycollection, Mode=OneWay}"
                SelectionChanged="ComboBox_SelectionChanged"
                />
        </DataTemplate>
    </controls:DataGridTemplateColumn.CellEditingTemplate>
    <controls:DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock
                Margin="10"
                HorizontalAlignment="Stretch"
                VerticalAlignment="Center"
                Text="{Binding p2, Mode=OneWay}"
                />
        </DataTemplate>
    </controls:DataGridTemplateColumn.CellTemplate>
</controls:DataGridTemplateColumn>

代码隐藏

public class P1
{
    public string Name { get; set; }
    public List<string> Mycollection { get; set; }
}

public class ITPP
{
    public P1 p1 { get; set; }
    public string p2 { get; set; }
}
private void CreateDataSource()
{
    items = new List<ITPP>();
    items.Add(new ITPP { p1 = new P1 { Name = "FirstP1", Mycollection = new List<string>() { "AA", "BB", "CC", "DD" } }, p2 = "CC" });
    items.Add(new ITPP { p1 = new P1 { Name = "SecondP1", Mycollection = new List<string>() { "EE", "FF", "GG", "HH" } }, p2 = "HH" });
    items.Add(new ITPP { p1 = new P1 { Name = "ThirdP1", Mycollection = new List<string>() { "II", "JJ", "KK", "LL" } }, p2 = "LL" });
    MyDataGrid.ItemsSource = items;
}
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    var mycombobox = sender as ComboBox;
    var p1collection = mycombobox.ItemsSource;
    foreach (var item in items)
    {
        if (item.p1.Mycollection == p1collection)
        {
            item.p2 = mycombobox.SelectedItem as string;
        }
    }
}