Reference to a TextBox inside a DataTemplate

前端 未结 3 2056
不知归路
不知归路 2021-01-23 10:27

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

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-23 10:44

    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:

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

提交回复
热议问题