wpf - 如何访问 DataGridTemplateColumn 中的控件以获取值?

标签 wpf c#-4.0

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

我正在使用此代码:

  <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?

最佳答案

您必须导航可视化树才能找到此元素。不久前我写了一个实用程序类,使用 Linq-to-VisualTree 使这更容易一些。你可以找到它如下:

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

关于wpf - 如何访问 DataGridTemplateColumn 中的控件以获取值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13226985/

相关文章:

c# - wpf - 更新的依赖属性不触发绑定(bind)

wpf - 如何以编程方式在 WPF TabControl 中选择 TabItem

C# await Task.Delay(1000);仅需 640ms 返回

c# - 在 HashSet<string> 性能中搜索字符串

c#-4.0 - 为什么 C# 不支持基于返回类型的重载

c# - .net 中的 Task.Delay 提前 125 毫秒触发

c# - 使用绑定(bind)设置用户控件中文本框的文本属性 - WPF

linq - LINQ 查询中子项的 OrderBy

c# - 使用泛型获取属性信息

c# - 对列表中的值进行分组并使用 C# 将输出转换为 JSON