大家好,我试图从DataGridTemplateColum获取名为“ txtQty”的TextBox值
这是代码,希望有人帮助我。...
.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());
答案 0 :(得分:0)
我的猜测是您正在尝试访问所选的行,这是https://stackoverflow.com/a/3913791/1449779的答案,也不要忘记将文本框绑定为DonBoitnott注释
答案 1 :(得分:0)
经过一番挣扎,我发现此解决方案很有帮助.....
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);