簡體   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