如何访问DataGrid.RowDetailsTemplate中的文本块内的数据

问题描述 投票:1回答:2

我怎样才能从TextBlock Name="txtSellerId"获取数据

<Grid Margin="10">
        <DataGrid Name="dgUsers" AutoGenerateColumns="False"  LoadingRowDetails="dgUsers_LoadingRowDetails">
            <DataGrid.Columns>
            </DataGrid.Columns>
            <DataGrid.RowDetailsTemplate >
                <DataTemplate >
<DockPanel Background="GhostWhite">
<grid>
<TextBlock Name="txtSellerId" Text="{Binding SellerId, StringFormat=d}" Grid.Column="1" Grid.Row="2" />
                        </Grid>

                    </DockPanel>
                </DataTemplate>

            </DataGrid.RowDetailsTemplate>

        </DataGrid>
c# wpf xaml
2个回答
1
投票

要以编程方式获取价值,您可以尝试此方法

dgUsers.LoadingRowDetails += dgUsers_LoadingRowDetails;

 void dgUsers_LoadingRowDetails(object sender, DataGridRowDetailsEventArgs e)
       {
         var textBlockValue = (e.DetailsElement as FrameworkElement).FindName("txtSellerId") as TextBlock;
       }    

0
投票

您可以将模型绑定到数据网格行,并在代码隐藏中使用它。

var user = (User)myDataGrid.SelectedItem;
var userId = user.Id;

一个简单的例子:DataGrid with row details

© www.soinside.com 2019 - 2024. All rights reserved.