【问题标题】:binding datagrid column to selecteditem property将 datagrid 列绑定到 selecteditem 属性
【发布时间】:2016-03-16 15:49:23
【问题描述】:

我有一个数据网格,如果单击一行,则会显示行详细信息。在行详细信息中是另一个名为 dgRights 的数据网格。

因此 dgRights 绑定到 SelectItem.Funds,其中funds 是一个自定义列表。 dgRights 显示 4 列,其中 3 列绑定得很好,但第四列不是(在我的代码中称为“Rights Sedol”)。

我希望将 Rights Sedol 列绑定到 selectedItem 的属性而不是 selectedItem.Funds,这可能吗?

我已经尝试了下面的代码行,但没有运气,

<DataGridTextColumn Header="Rights Sedol" Binding="{Binding SelectedItem.NewSecurity.Sedol, RelativeSource={RelativeSource AncestorType=Window}}/>                                

第二

<DataGridTextColumn Header="Rights Sedol" Binding="{Binding SelectedItem.NewSecurity.Sedol, RelativeSource={RelativeSource AncestorType=DataGrid}}/>   

我还尝试将数据网格的 ItemSource 从 SelectedItem.Funds 更改为仅 SelectItem 并将其他 3 个工作列更改为 Funds.Code 等,但这根本没有在数据网格中显示任何数据。所以不知道该怎么办?

App.xaml - 我的行详细信息数据模板

<DataTemplate x:Key="DG_RowDetailRGHTSHist">
        <Grid x:Name="RowDetailGrid"            
              Margin="5"
              HorizontalAlignment="Left">
            <Border HorizontalAlignment="Left"
                    VerticalAlignment="Top"
                    Height="250"
                    CornerRadius="5">
                <Border.Background>
                    <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                        <GradientStop Offset="0" Color="Transparent"/>
                        <GradientStop Offset="1" Color="Transparent"/>
                    </LinearGradientBrush>
                </Border.Background>
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="4*"/>
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="400"/>
                        <ColumnDefinition Width="200"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <TextBlock Grid.Row="0"
                               Grid.Column="0"
                               Margin="5,5,5,5"
                               HorizontalAlignment="Left"
                               FontSize="12"
                               FontWeight="Bold"
                               Foreground="Black" 
                               Text="Fund Summary">
                    </TextBlock>
                    <DataGrid Grid.Row="1" Grid.Column="0"  Grid.ColumnSpan="2"
                              ItemsSource="{Binding SelectedItem.Funds,  RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"                                                                  
                              RowStyle="{StaticResource DG_Row}"  
                              ColumnHeaderStyle="{StaticResource DG_ColumnHeader}" 
                              RowHeaderStyle="{StaticResource DG_RowHeaderNested}"
                              CellStyle="{StaticResource DG_Cell}" 
                              Background="Silver"
                              HorizontalGridLinesBrush="LightGray"
                              VerticalGridLinesBrush="LightGray"
                              CanUserAddRows="False"
                              CanUserDeleteRows="False"
                              Margin="50,5,5,20"
                              AutoGenerateColumns="False">
                        <DataGrid.Columns>
                            <DataGridTextColumn Header="Fund Code" Binding="{Binding Code}" IsReadOnly="True" MinWidth="75"/>
                            <DataGridTextColumn Header="Fund Code SS" Binding="{Binding CodeSS}" IsReadOnly="True" MinWidth="75"/>
                            <DataGridTextColumn Header="Rights Sedol" Binding="{Binding SelectedItem.NewSecurity.Sedol, RelativeSource={RelativeSource AncestorType=Window}}" IsReadOnly="True" MinWidth="75"/>                                
                            <DataGridTextColumn Header="Number of Rights" Binding="{Binding CurrentNominal, Mode=TwoWay, StringFormat={}{0:N0}}" IsReadOnly="True"/>
                        </DataGrid.Columns>
                    </DataGrid>
                </Grid>
            </Border>
        </Grid>
    </DataTemplate>

【问题讨论】:

  • 试一试:{RelativeSource AncestorType=DataGrid, AncestorLevel=2}} AncestorLevel=2 部分告诉它向上转到第二个 DataGrid。
  • 谢谢你的作品!如果您将其作为答案发布,很乐意将其标记为正确

标签: c# wpf xaml datagrid


【解决方案1】:

您想要访问第二个数据网格,因此您需要指定 AncestorLevel=2

{RelativeSource AncestorType=DataGrid, AncestorLevel=2}}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-10
    • 2011-04-30
    • 1970-01-01
    • 2012-10-12
    • 2012-02-12
    • 2013-07-27
    • 2014-10-05
    • 2011-11-16
    相关资源
    最近更新 更多