简体   繁体   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?

How can I bind the ItemsSource of a Combobox column to a collection that is a sub-property of a property on the same row?如何将 Combobox 列的 ItemsSource 绑定到作为同一行属性子属性的集合? In other words, the DataGrid is bound to a collection of items of a class the contains Property1 and Property2.换句话说,DataGrid 绑定到包含 Property1 和 Property2 的类的项目集合。 So the DataGrid has two columns, Property1 and Property2.所以DataGrid 有两列,Property1 和Property2。 Property1 has a sub-property that is an Observable Collection. Property1 有一个子属性,它是一个 Observable 集合。 The column for Property2 is a Combobox column that should use Property1's Observable Collection as its ItemsSource. Property2 的列是一个组合框列,它应该使用 Property1 的 Observable 集合作为其 ItemsSource。

I'm loading the grid's source data via EFCore.我正在通过 EFCore 加载网格的源数据。 I have made sure to use ".Include" to be sure Property1's Observable Collection is getting loaded into memory.我确保使用“.Include”来确保 Property1 的 Observable 集合被加载到内存中。 I'm wondering if the issue is that the UI is not made aware that Property1's Observable Collection has been updated when it is loaded from the database?我想知道问题是否是 UI 在从数据库加载时没有意识到 Property1 的 Observable 集合已更新? If that's the issue, how could I correct this?如果这是问题,我该如何纠正?

I need this collection to serve as the ItemsSource for the Property2 column shown below.我需要这个集合作为下面显示的 Property2 列的 ItemsSource。 I've tried using relative source to get the datacontext of the grid, but it is not working.我试过使用相对源来获取网格的数据上下文,但它不起作用。

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

I've also tried this:我也试过这个:

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

Thanks.谢谢。

Derive official processing method , the better way is set DataGridComboBoxColumn in the code behind.派生官方处理方法,更好的方法是在后面的代码中设置DataGridComboBoxColumn Give DataGridComboBoxColumn a tag name and find the column with tag like the following then set DataGridComboBoxColumn ItemsSource .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 Code Xaml 代码

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

DataGridComboBoxColumn ItemsSource property could not access data source sub property directly, so we need make a list property for the Page class that uses to store Property1.MyCollection . DataGridComboBoxColumn ItemsSource 属性无法直接访问数据源子属性,因此我们需要为用于存储Property1.MyCollection的 Page 类创建一个列表属性。 If you already make MyCollection propety for the Page class, you could also use x:bind to access like the following.如果您已经为 Page 类创建了 MyCollection 属性,您还可以使用 x:bind 进行访问,如下所示。

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

Update更新

If the Property1.MyCollection is not constant list, you could try to use DataGridTemplateColumn custom column cell and bind the data source like the following如果 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>

Code behind背后的代码

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.

相关问题 DataGrid中的ComboBox:ItemSource绑定到动态资源,如何设置默认行? - ComboBox in DataGrid: ItemSource bound to dynamic resource, how to set default row? 根据同一行中的另一个组合框列过滤数据网格中的组合框 - Filter a combobox in a datagrid based on another combobox column in the same row 如何使DataGrid行中的ComboBox唯一? - How can I make a ComboBox in a DataGrid row unique? 设置 ItemSource 后向 DataGrid 添加列/设置 ItemSource 后向 DataGrid 的每一行添加字段 - Adding a column to a DataGrid after the ItemSource is set / adding a field to every row of a DataGrid after the ItemSource is set 如何为datagrid的NewItemPlaceholder行添加模板? - How can I add a template for datagrid's NewItemPlaceholder row? 当ItemsSource为空时,如何确保WPF DataGrid至少显示一行? (可编辑的ComboBox单元格) - How can I make sure my WPF DataGrid shows at least one row when ItemsSource is empty? (Editable ComboBox cells) 将 WPF Datagrid 列设置为 Combobox itemsource - Set WPF Datagrid column as a Combobox itemsource 当我在WPF中选择一行数据网格时,如何在组合框中显示一个值? - How to show a value in combobox when i select a row of datagrid in wpf? 在ItemSource刷新上删除的DataGrid行突出显示 - DataGrid Row Highlight Being Removed on ItemSource Refresh WPF ItemSource DataGrid TreeViewItem额外的行错误 - WPF ItemSource DataGrid TreeViewItem extra row bug
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM