问题描述
我有一个包含行的数据网格,其中包含从Web服务器读取的数据和要写入Web服务器的值。我编写了让用户在相应的列中输入数字并单击相邻文本框的值;
I have a datagrid of rows which contain data read from a web server and values I want to write into a webserver. I write the values in getting the user to input a number into the appropriate column and click an adjacent text box;
<DataGrid x:Name="datagridDERControl" HorizontalAlignment="Center" VerticalAlignment="Center" Background="#FF322D2D" Height="382" Margin="10,78,10,10" Width="972" ItemsSource="{Binding Path=NFDataSource, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<DataGrid.Columns>
<DataGridTemplateColumn Width="100" Header="Write Set Point">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox Width="100" Text="{Binding Path=WriteSetPoint, Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged}"></TextBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Width="100" Header="Global Trip">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Name="buttonGlobalTrip" Width="100" Click="buttonGlobalTrip_Click"></Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid
如何提取每个特定的文本框字符串在我的视图模型中使用的行。
How do I extract the specific textbox string per row to use in my view model.
推荐答案
在问题被忽略了相关细节的情况下,总是很难回答这个问题作者。但是,我会尝试!
It's always difficult to answer a question where the relevant details have been omitted by the question author. However, I shall try!
您将数据(大概)绑定了名为 NFDataSource
的集合属性到您的 DataGrid.ItemsSource
属性。那就是表示 DataGrid
中的数据的集合,因此要提取特定值,您需要查看集合中的数据项。
You have data bound (presumably) a collection property named NFDataSource
to your DataGrid.ItemsSource
property. That is the collection that represents the data in your DataGrid
, so to 'extract' a specific value, you need to look into your data items in your collection.
DataGrid
类中的一个方便属性是 SelectedItem
属性。这使您可以将对象(与 NFDataSource
集合中的对象相同类型的对象)数据绑定到此属性,从而访问当前在其中选择的行后面的数据对象UI:
One handy property in the DataGrid
class is the SelectedItem
property. this enables you to data bind an object (of the same type as those in your NFDataSource
collection) to this property, which accesses the data object behind the row that is currently selected in the UI:
<DataGrid ItemsSource="{Binding NFDataSource}" SelectedItem="{Binding SelectedItem}" />
现在,您可以使用 SelectedItem
属性访问 DataGrid
中所选行的值:
Now you can utilise your SelectedItem
property to access the values from the selected row in the DataGrid
:
string someValue = SelectedItem.SomeProperty;
这篇关于从DataGrid MVVM中的TextBox中读取文本(WPF数据绑定)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!