简体   繁体   English

对DataTemplate内部的TextBox的引用

[英]Reference to a TextBox inside a 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). 如何获得对仅在DataTemplate内部定义的TextBox的引用(假设我刚刚将此DataTemplate应用到了网格中的某些单元格)。

So far I'm using the sender in the TextBox events to retrieve this. 到目前为止,我在TextBox事件中使用发件人来检索此内容。

Thanks, rui 谢谢,瑞

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: 您还可以尝试以下一种选择:

in .xaml: 在.xaml中:

    <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" />

in .xaml.cs 在.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!! 希望这可以帮助!!

Edit: 编辑:

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. 如果要使用x:Name,则还需要至少获取ContentPresenter,而要获取ContentPresenter,则需要遍历元素树。 The updates you need to make are: 您需要进行的更新是:

in .xaml: 在.xaml中:

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

in .xaml.cs 在.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!! 希望这可以帮助!!

Sorry, but you're doing it wrong. 抱歉,您做错了。
There's no good reason why you should have a reference to elements inside a DataTemplate IMO. 没有充分的理由说明为什么应该引用DataTemplate IMO中的元素。 Moreover, there's really no good reason for you to ever register for a Control Event. 此外,您确实没有充分的理由注册控制事件。

As part of the MVVM architecture we started looking at Data and Interactions. 作为MVVM体系结构的一部分,我们开始研究数据和交互。
On the Data side - everything is databound to the ViewModel. 在数据方面-所有数据都绑定到ViewModel。
On the interactions side - Using ICommands all events are wired up for commands. 在交互方面-使用ICommands,所有事件都连接到命令。

So, in your TextBox example - why are you listening to textbox events? 因此,在您的TextBox示例中-为什么要听文本框事件? Use TwoWay DataBinding to learn when TextBox text change. 使用TwoWay DataBinding了解TextBox文本何时更改。
In another example in which events are justified, like button.Click? 在另一个证明事件合理的示例中,例如button.Click? Use Button.Command="{Binding myCommand}" to have commands handle events. 使用Button.Command =“ {Binding myCommand}”使命令处理事件。

The reason you're running into issues is because you're trying to force a round peg in a square hole. 遇到问题的原因是因为您试图将圆钉钉在方孔中。

-- Justin -贾斯汀

I agree with Justin. 我同意贾斯汀。

But if for some reason binding to some property is problematic and you still need reference to a control inside data template in SILVERLIGHT ( above solution is suitable for WPF components ) you can do as follow: 但是,如果由于某种原因绑定到某些属性有问题,并且您仍然需要引用SILVERLIGHT中的数据模板内部的控件(上述解决方案适用于WPF组件),则可以执行以下操作:

TextBox textBox = null;

   if (datagrid.SelectedItem != null)
      textBox = datagrid.Columns[1].GetCellContent(datagrid.SelectedItem) as TextBox;

   if (textBox != null)
      MessageBox.Show(textBox.Text);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM