如何在DataGridTemplateColumn中访问控件以获取价值? [英] How to access control in DataGridTemplateColumn to get value?

查看:72
本文介绍了如何在DataGridTemplateColumn中访问控件以获取价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在DataGridTemplateColumn中访问控件以获取价值?

How to access control in DataGridTemplateColumn to get value ?

我正在使用以下代码:

  <DataGrid HeadersVisibility="None"  Name="dgUser" Grid.Row="0" Grid.Column="0" AutoGenerateColumns="False" ItemsSource="{Binding}"
 CanUserAddRows="False" CanUserDeleteRows="False" CanUserSortColumns="False">
                                <DataGrid.Columns>
                                    <DataGridTemplateColumn Width="*" Header="" IsReadOnly="True">
                                        <DataGridTemplateColumn.CellTemplate>
                                            <DataTemplate>
                                                <StackPanel Orientation="Horizontal"  Margin="10,0,0,0" Grid.Column="0"  Name="partcount">
                                                    <TextBlock Margin="0,0,5,0" Text="Count"/>
                                                    <TextBox Margin="0,0,5,0" MinWidth="50" Width="Auto" Name="txtcount" Text="{Binding Count}"/>
                                                </StackPanel>
                                            </DataTemplate>
                                        </DataGridTemplateColumn.CellTemplate>
                                    </DataGridTemplateColumn>
       </DataGrid.Columns>
                            </DataGrid>

如何访问txtcount?

how to access txtcount ?

推荐答案

您将必须在视觉树中导航才能找到此元素。不久前,我写了一个实用程序类,使用 Linq-to-VisualTree 如下所示:

You will have to navigate the visual tree to find this element. I wrote a utility class a while back that makes this a little easier, using Linq-to-VisualTree you can find it as follows:

TextBox tb = dgUser.Descendants<TextBox>()
                   .OfType<TextBox>()
                   .Where(t => t.Name == "txtcount")
                   .Single();

这篇关于如何在DataGridTemplateColumn中访问控件以获取价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆