WPF DataGrid主/详细信息:如何在详细信息中显示不同ItemSource中的主属性

时间:2015-08-18 18:36:01

标签: wpf datagrid master-detail

我有一个对象层次结构:订单有分配。我的DataGrid有一个订单的ItemSource,然后显示每个网格行中每个订单的一些细节。单击网格行后,将在详细信息窗格中显示其他详细信息,包括每个订单的分配列表。我需要做的是以某种方式引用回主记录并为每个分配拉出一些细节(最终会重复)。如果我尝试将RelativeSource返回到DataGrid,我不能引用该属性,因为该源是一个完整的集合,我需要从该集合的项目中获取值。

以下是XAML I的简化版本。基本上,我需要从子(订单)记录中的每个Allocation项目绑定到主记录中的QuantityTypeDescription。

        <DataGrid ItemsSource="{Binding Orders}" AutoGenerateColumns="False" IsReadOnly="True" HorizontalScrollBarVisibility="Auto">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Trade" Binding="{Binding TransactionTypeDescription}" Width="*"/>
            <DataGridTextColumn Header="Adjustment" Binding="{Binding QuantityTypeDescription}" Width="Auto"/>
        </DataGrid.Columns>
        <DataGrid.RowDetailsTemplate>
            <DataTemplate>
                <!--Order Summary-->
                <StackPanel Orientation="Vertical">
                    <StackPanel Background="DarkSlateGray" Orientation="Vertical">
                        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
                            <TextBlock Text="{Binding TransactionTypeDescription}" FontWeight="Bold" Margin="5,0,0,0" FontSize="14" Foreground="White"/>
                        </StackPanel>
                        <TextBlock Name="Name" Text="{Binding SecurityName}" HorizontalAlignment="Center" FontWeight="Bold" Foreground="White" Background="DarkSlateGray"/>
                    </StackPanel>
                    <StackPanel Orientation="Vertical">
                        <ItemsControl ItemsSource="{Binding Allocations}">
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <StackPanel Orientation="Horizontal">

注意:在这里,我需要恢复一个级别才能获得QuantityTypeDescription

                                    <!--<TextBlock Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=DataGrid, AncestorLevel=1}, Path=QuantityTypeDescription}" Margin="5,0,0,0"/>-->
                                    <TextBlock Text="{Binding Amount}" Margin="5,0,0,0"/>
                                </StackPanel>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
                </StackPanel>
            </StackPanel>
        </DataTemplate>
    </DataGrid.RowDetailsTemplate>
</DataGrid>

1 个答案:

答案 0 :(得分:2)

使用ElementName绑定来引用您的主DataGrid SelectedItem

 <DataGrid Name="MainDataGrid" ItemsSource="{Binding Orders}" AutoGenerateColumns="False" IsReadOnly="True" HorizontalScrollBarVisibility="Auto">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Trade" Binding="{Binding TransactionTypeDescription}" Width="*"/>
            <DataGridTextColumn Header="Adjustment" Binding="{Binding QuantityTypeDescription}" Width="Auto"/>
        </DataGrid.Columns>
        <DataGrid.RowDetailsTemplate>
            <DataTemplate>
                <!--Order Summary-->
                <StackPanel Orientation="Vertical">
                    <StackPanel Background="DarkSlateGray" Orientation="Vertical">
                        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
                            <TextBlock Text="{Binding TransactionTypeDescription}" FontWeight="Bold" Margin="5,0,0,0" FontSize="14" Foreground="White"/>
                        </StackPanel>
                        <TextBlock Name="Name" Text="{Binding SecurityName}" HorizontalAlignment="Center" FontWeight="Bold" Foreground="White" Background="DarkSlateGray"/>
                    </StackPanel>
                    <StackPanel Orientation="Vertical">
                        <ItemsControl ItemsSource="{Binding Allocations}">
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <StackPanel Orientation="Horizontal">
                                        <TextBlock Text="{Binding ElementName=MainDataGrid,Path=SelectedItem.QuantityTypeDescription}" Margin="5,0,0,0"/>
                                        <TextBlock Text="{Binding Amount}" Margin="5,0,0,0"/>
                                    </StackPanel>
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                        </ItemsControl>
                    </StackPanel>
                </StackPanel>
            </DataTemplate>
        </DataGrid.RowDetailsTemplate>
    </DataGrid>