【问题标题】:Get textbox name in wpf在wpf中获取文本框名称
【发布时间】:2014-03-11 07:48:28
【问题描述】:

我是 WPF 的新手,所以我认为这是一个简单的问题。问题是我无法获取文本框名称。文本框位于 GridView 内。有什么解决办法吗? 文本框位于 DataTemplate 下。

XAML

<GridViewColumn.CellTemplate >
    <DataTemplate >
        <StackPanel >
            <TextBox x:Name="txtPurchasePrice" 
                     Width="60" 
                     TextChanged="txtPurchasePrice_TextChanged" />
        </StackPanel>
    </DataTemplate>
</GridViewColumn.CellTemplate>

【问题讨论】:

  • 这就是您要找的东西吗? stackoverflow.com/questions/16997951/…
  • 如果文本框在网格视图中,则无法将文本框直接放入代码隐藏中,您需要先从网格视图中选择一个原始文件,然后从该原始文件中找到文本框。

标签: c# wpf textbox


【解决方案1】:

不幸的是,有一种方法可以像访问命名对象一样简单。一种选择是遍历父控件的子对象并根据已知值检查文本字段

private void Button_Click(object sender, RoutedEventArgs e)
{
    GetTextBox(datagrid);
}

public void GetTextBox(Visual visual)
{
    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(visual); i++)
    {
        Visual childVisual = (Visual) VisualTreeHelper.GetChild(visual, i);
        if (childVisual is TextBox)
            MessageBox.Show("textbox Found");
        // Recursively enumerate children of the child visual object.
    }
}

【讨论】:

    猜你喜欢
    • 2023-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多