繁体   English   中英

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

[英]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 绑定到包含 Property1 和 Property2 的类的项目集合。 所以DataGrid 有两列,Property1 和Property2。 Property1 有一个子属性,它是一个 Observable 集合。 Property2 的列是一个组合框列,它应该使用 Property1 的 Observable 集合作为其 ItemsSource。

我正在通过 EFCore 加载网格的源数据。 我确保使用“.Include”来确保 Property1 的 Observable 集合被加载到内存中。 我想知道问题是否是 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}"

谢谢。

派生官方处理方法,更好的方法是在后面的代码中设置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 属性无法直接访问数据源子属性,因此我们需要为用于存储Property1.MyCollection的 Page 类创建一个列表属性。 如果您已经为 Page 类创建了 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;
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM