WPF从datagridtemplatecolumn获取TextBox值 [英] WPF get TextBox Value from datagridtemplatecolumn
本文介绍了WPF从datagridtemplatecolumn获取TextBox值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,我正在尝试从DataGridTemplateColum中获取名为 txtQty的TextBox值。
Hello guys I am trying to get TextBox Named "txtQty" value from DataGridTemplateColum
这是代码,希望有人对我有所帮助...。
Here is the code, Hope someone Helps me....
.XML
<DataGrid x:Name="dataGridMain">
<DataGrid.Columns>
<DataGridTextColumn Header="Id" Binding="{Binding Id}" IsReadOnly="True" Visibility="Hidden"/>
<DataGridTextColumn Header="Name" Binding="{Binding PName}" IsReadOnly="True"/>
<DataGridTemplateColumn Header="Qty" >
<DataGridTemplateColumn.CellTemplate >
<DataTemplate >
<StackPanel Orientation="Horizontal">
<TextBox x:Name="txtQty"/>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
我尝试使用此代码
DataRowView dt = dataGridMain.SelectedItem as DataRowView;
String value = dt["Qty"].ToString());
推荐答案
经过一番努力后,我发现此解决方案很有帮助... ..
After some struggle I found this solution helpful.....
int i=5; //Set this equal to desired column index....
ContentPresenter myCp = dataGridMain.Columns[i].GetCellContent(dataGridMain.SelectedItem) as ContentPresenter;
var myTemplate = myCp.ContentTemplate;
TextBox mytxtbox = myTemplate.FindName("txtQty", myCp) as TextBox;
MessageBox.Show(mytxtbox.Text);
这篇关于WPF从datagridtemplatecolumn获取TextBox值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文