简体   繁体   English

从DataGrid MVVM中的TextBox中读取文本(WPF数据绑定)

[英]Read Text From TextBox in DataGrid MVVM (wpf databinding)

I have a datagrid of rows which contain data read from a web server and values I want to write into a webserver. 我有一个包含行的datagrid,其中包含从Web服务器读取的数据和要写入Web服务器的值。 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! 但是,我会尝试!

You have data bound (presumably) a collection property named NFDataSource to your DataGrid.ItemsSource property. 您已经将数据(大概是)名为NFDataSource的集合属性NFDataSource到了DataGrid.ItemsSource属性。 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数据的集合,因此要“提取”特定值,您需要查看集合中的数据项。

One handy property in the DataGrid class is the SelectedItem property . DataGrid类中的一个便捷属性是SelectedItem属性 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: 这使您可以将对象(与NFDataSource集合中的对象类型相同)的数据绑定到此属性,该属性访问UI中当前所选行后面的数据对象:

<DataGrid ItemsSource="{Binding NFDataSource}" SelectedItem="{Binding SelectedItem}" />

Now you can utilise your SelectedItem property to access the values from the selected row in the DataGrid : 现在,您可以利用SelectedItem属性访问DataGrid所选行的值:

string someValue = SelectedItem.SomeProperty;

As you tagged this with MVVM and databinding, I'll assume you're using these and have just got muddled. 当您使用MVVM和数据绑定对其进行标记时,我假设您正在使用它们,并且已经一头雾水。

"I have a datagrid of rows which contain data read from a web server and values I want to write into a webserver." “我有一个包含行的数据网格,其中包含从Web服务器读取的数据和要写入Web服务器的值。”

So your viewmodel has a property, which is a collection of a custom class, that represents data fetched from a webservers. 因此,您的viewmodel具有一个属性,该属性是一个自定义类的集合,该属性表示从Web服务器获取的数据。

"I write the values in getting the user to input a number into the appropriate column and click an adjacent text box" “我编写了让用户在相应的列中输入数字并单击相邻的文本框的值”

So this VM property is two-way bound to a datagrid, so that each item in the collection represents 'one row', and the properties on those items represent your 'columns'. 因此,此VM属性是双向绑定到数据网格的,因此集合中的每个项目都代表“一行”,而这些项目上的属性则代表您的“列”。 The user can make changes to the UI displayed values, and because of the two way databinding the VM property is also updated. 用户可以更改UI显示的值,并且由于两种方式的数据绑定,VM属性也被更新。

"How do I extract the specific textbox string per row to use in my view model." “如何提取每行要在视图模型中使用的特定文本框字符串。”

Why do you need the specific textbox string, if it is databound to a property (or rather to a property on a class contained in a collection) in your VM anyway? 无论如何,如果特定的文本框字符串已数据绑定到VM中的属性(或者更确切地说是集合中包含的类的属性),为什么还需要它? If you've set up your VM this way, and are using databinding, you rarely need to worry about UI specific things such as which row in a datagrid is clicked. 如果以这种方式设置了VM,并且正在使用数据绑定,则几乎不必担心UI特定的事情,例如,单击数据网格中的哪一行。

As Sheridan points out though, you can also bind to properties on the datagrid such as SelectedItem so that you can perform additional operations beyond just reading/writing data. 正如Sheridan指出的那样,您还可以绑定到数据网格上的属性(例如SelectedItem以便除了读取/写入数据之外还可以执行其他操作。 SelectedItem for your datagrid will be of the type that populates your VM collection, so will have the appropriate properties. 您的数据网格的SelectedItem的类型将填充您的VM集合,因此将具有适当的属性。

For example, if your VM collection is an IQueryable<Person> and that is bound to the ItemsSource of the datagrid then the SelectedItem will be of type Person . 例如,如果您的VM集合是IQueryable<Person>且绑定到数据网格的ItemsSource ,则SelectedItem的类型为Person You could then have a VM property called SelectedPerson which is bound to that SelectedItem , and access things like SelectedPerson.Name etc. 然后,您可以拥有一个名为SelectedPerson的VM属性,该属性绑定到该SelectedItem ,并访问SelectedPerson.Name等内容。

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

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