引用一个DataTemplate内的文本框 [英] Reference to a TextBox inside a DataTemplate

查看:111
本文介绍了引用一个DataTemplate内的文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何获得一个引用,这只是内部的一个DataTemplate定义的文本框(假设我刚刚应用这个DataTemplate中的一些细胞在网格中)。

How do I get a reference to a TextBox that's only defined inside a DataTemplate (assuming that I've just applied this DataTemplate to some cell in a grid).

到目前为止,我使用的文本框事件的发送者检索此。

So far I'm using the sender in the TextBox events to retrieve this.

谢谢, 瑞

推荐答案

为了得到里面的数据模板控件的参考,处理事件,然后使用发件人可用的选项之一。还有一个选项,你可以试试:

For getting the reference of a control inside a Data Template, handling the event and then using the sender is one of the available option. There is one more option that you can try:

    <toolkit:DataGrid Name="datagrid" Margin="0,0,0,28" AutoGenerateColumns="False">
        <toolkit:DataGrid.Columns>
            <toolkit:DataGridTextColumn Header="First Name" Binding="{Binding FirstName}"/>
            <toolkit:DataGridTemplateColumn Header="Last Name">
                <toolkit:DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBox Text="{Binding LastName}"/>
                    </DataTemplate>
                </toolkit:DataGridTemplateColumn.CellTemplate>
            </toolkit:DataGridTemplateColumn>
        </toolkit:DataGrid.Columns>
    </toolkit:DataGrid>
    <Button Height="22" VerticalAlignment="Bottom" Click="Button_Click" />

在.xaml.cs

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

    public void InitializeMouseHandlersForVisual(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.

            InitializeMouseHandlersForVisual(childVisual);
        }
    }

希望这有助于!

Hope this helps!!

如果你想用x:名称,然后还需要至少得到内容presenter和获取内容presenter你需要通过元素树。你需要做的更新是:

if you want to use x:Name then also you need to at least get the ContentPresenter and for getting ContentPresenter you need to go through the element tree. The updates you need to make are:

    <DataTemplate>
        <TextBox x:Name="text" Text="{Binding LastName}"/>
     </DataTemplate>

在.xaml.cs

    public void InitializeMouseHandlersForVisual(Visual visual)
    {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(visual); i++)
        {
            Visual childVisual = (Visual) VisualTreeHelper.GetChild(visual, i);
            ContentPresenter myContentPresenter = childVisual as ContentPresenter;
            if (myContentPresenter != null)
            {
                // Finding textBlock from the DataTemplate that is set on that ContentPresenter
                DataTemplate myDataTemplate = myContentPresenter.ContentTemplate;
                if (myDataTemplate != null)
                {
                    TextBox myTextBox = (TextBox)myDataTemplate.FindName("text", myContentPresenter);
                    MessageBox.Show("textbox Found");
                }
            }
            InitializeMouseHandlersForVisual(childVisual);
        }
    }

希望这有助于!

Hope this helps!!

这篇关于引用一个DataTemplate内的文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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