Alfred Angkasa Alfred Angkasa - 1 year ago 183
C# Question

WPF binding data from datagrid to textbox

i'm new to WPF and still learn on it. I create a sample application and connect to the database. After i select data to the database, i can show it into my datagrid. Now, my concern is i want to bind to textbox depends on the row from my datagrid.

so, whenever i click or select row in my datagrid, i bind the value to textbox. i already do some google and tried. but still failed. is there any solution? thanks.

here is my application picture.
enter image description here

how is the xaml look like to bind for example row no 3 to my textbox? is there any class i should implement? because in windows form i just need to call cellclick. thanks.

Answer Source

I'm always binding SelectedItem (datagrid Property) to a Property in my ViewModel. And then you can bind the Controls to this property. You also can achieve this without a viewmodel only with xaml:

    <TextBox Text="{Binding SelectedItem.Name, ElementName=myDataGrid}"/>
    <DataGrid x:Name="myDataGrid" />