Read Text From TextBox in DataGrid MVVM (wpf databinding)

吃可爱长大的小学妹 提交于 2020-01-15 12:34:29

问题


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.


回答1:


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. 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.

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}" />

Now you can utilise your SelectedItem property to access the values from the selected row in the DataGrid:

string someValue = SelectedItem.SomeProperty;



回答2:


As you tagged this with MVVM and databinding, I'll assume you're using these and have just got muddled.

"I have a datagrid of rows which contain data read from a web server and values I want to write into a webserver."

So your viewmodel has a property, which is a collection of a custom class, that represents data fetched from a webservers.

"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'. The user can make changes to the UI displayed values, and because of the two way databinding the VM property is also updated.

"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? 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.

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. SelectedItem for your datagrid will be of the type that populates your VM collection, so will have the appropriate properties.

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. You could then have a VM property called SelectedPerson which is bound to that SelectedItem, and access things like SelectedPerson.Name etc.



来源:https://stackoverflow.com/questions/24117030/read-text-from-textbox-in-datagrid-mvvm-wpf-databinding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!