简体   繁体   English

绑定ObservableCollection <Tuple<Object, List<Object> &gt;&gt;到DataGrid

[英]Binding ObservableCollection<Tuple<Object, List<Object>>> to DataGrid

I have an ObservableCollection with a Tuple and want to bind it to my DataGrid. 我有一个带元组的ObservableCollection,并想将其绑定到我的DataGrid。 This is the ObservableCollection: 这是ObservableCollection:

ObservableCollection<Tuple<NetworkItem, List<NetworkItem>>> ListNetworkItems = new ObservableCollection<Tuple<NetworkItem, List<NetworkItem>>>();

And here is my DataGrid: 这是我的DataGrid:

<DataGrid x:Name="NetworkDataGrid" 
          ItemsSource="{Binding ListNetworkItems}" >

    <DataGrid.Columns>

        <DataGridTemplateColumn Header="Sender">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Item1.Device.Name, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>

        <DataGridTemplateColumn Header="Receiver">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Item2.Device.Name, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>

    </DataGrid.Columns>

</DataGrid>

The problem is, how can I bind the second item in my ObservableCollection? 问题是,如何绑定ObservableCollection中的第二个项目? This is a list of several devices which belows to the item1 in the ObservableCollection. 这是ObservableCollection中item1下面的几个设备的列表。 My DataGrid should look like: 我的DataGrid应该看起来像:

Sender      Receiver
Device 1    Device 2
            Device 3
            Device 5
Device 2    Device 1
Device 3    Device 2
            Device 4

You need another list control of some kind in the cells in column 2 to display your sub-list. 您需要在第2列的单元格中使用某种其他列表控件来显示子列表。

Something like this should work: 这样的事情应该起作用:

<DataGridTemplateColumn Header="Receiver">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <ItemsControl ItemsSource="{Binding Item2}">
                <ItemsControl.ItemTemplate>
                        <DataTemplate>
                                <TextBlock Text="{Binding Device.Name, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" />
                        </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

This will create an ItemsControl in every cell in your Receiver column to hold the list of items within the sub-list. 这将在Receiver列的每个单元格中创建一个ItemsControl ,以保存子列表中的项目列表。

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

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