views:

2141

answers:

2

hi frnds,

I am using a WPF application, in which i placed a listview control which has 3 gridview columns. First Gridview column has a label Control and the remaining 2 gridview columns has textbox control.

Now my problem is if the user enters a value in the first Gridview textbox column, the second gridview textbox column should be updated with some value. Is there any way to do that.

I am filling the list view with a datatable from code behind file.

And also is there any way to get the value of the label control in the 1st gridview column.

Thanks in advance urs Frnd :)

A: 

You can bind two TextBox's together by using an ElementName binding. The Grid column they are in is irrelevant. The example below will cause the text in textBox2 to automatically update to the text entered in textBox1.

    <TextBox Text="TextBox" Name="textBox1" />
    <TextBox Text="{Binding ElementName=textBox1, Path=Text}" Name="textBox2"/>

If the second textbox needed to be udpated with a different value you could remove the above binding and instead do it programatically:

textBox2.Text = textBox1.Text + " some text";

To get the value of the Label control you need to assign it a name and then you can access it in your code behind file using that name.

XAML:

<Label Name="myLabel" Content="Label"/>

C#:

 // Assuming you have assigned text content to the Label as in the above XAML.
 string labelContent = myLabel.Content as string;
sipwiz
This would just set textbox2 to the value entered in textbox1.. I think the OP wants to compute another value based on the value of textbox1 and display it in textbox2
Gishu
You could be right. I added a small note for that.
sipwiz
A: 

From what I see the WPF GridView is just a view mode (look n feel) for the ListView control.. As with most things in WPF, I'd advise you to do this with the ViewModel pattern.

  • What that means is that you have create a ViewModel object for each row in the Grid. The real model would be your DataTable row. The View model is seeded with data from the real model.. i.e. you create a new ViewModel instance from each row in your DataTable. You then populate the Grid with ViewModel objects
  • The View model exposes properties for each value you need to display in the actual view. So in this case your ViewModel would expose 3 properties (for each column).
  • Wire up your UI Column to bind to the respective properties in the ViewModel.
  • Now your ViewModel.Property1 would contain the logic of updating Property2 with the right computed value on a change. Both of them should trigger off PropertyChanged notifications and the GridView should reflect the updated values in both cells.
Gishu