I am having a datagrid contains 2 textboxes (txtBasicAjustmentFactor and txtBasicAdjustedValue) and a textblock (OriginalValue). On textchange event of 1st textbox (txtBasicAjustmentFactor) I
want to update the second textbox(txtBasicAdjustedValue) with multiplication of textblock(OriginalValue) and 1st textbox (txtBasicAdjustedValue).
var dataGridrow =
DataGridRow.GetRowContainingElement(sender
as FrameworkElement);
//Or get clicked index
var rowIndex =
DataGridRow.GetRowContainingElement(sender
as FrameworkElement).GetIndex();
//
//Code Rquired Here.
}
using rowindex I ahve to find the textboxes and update the text of second text box. But How...........................
You don't need the row index for this. The DataContext of the TextBox is the item your editing. For the calculated fields, you can do this in a couple ways:
Have calculated properties in your item that you update. Implement INotifyPropertyChanged and any updates will show up in your UI
Use binding converters that take your item and return the calculated value
I'm using binding converters that take my item and return the calculated value, but how do I update the converters value upon changing the values in the binding?
Because the value the converter is taking is the entity and its aggregating over its properties and displaying a total. Now this total is not updating itself upon changing the properties in the entity.
DeepakGoel
Member
1 Points
4 Posts
Update one Textbox on textchange of another textbox in DataGrid.
Feb 25, 2009 10:53 AM | LINK
HI
I am having a datagrid contains 2 textboxes (txtBasicAjustmentFactor and txtBasicAdjustedValue) and a textblock (OriginalValue). On textchange event of 1st textbox (txtBasicAjustmentFactor) I want to update the second textbox(txtBasicAdjustedValue) with multiplication of textblock(OriginalValue) and 1st textbox (txtBasicAdjustedValue).
XAML CODE: -
<data:DataGrid VerticalContentAlignment="Top" Grid.Row="1" FontSize="10" x:Name="dgBasic" AutoGenerateColumns="False" > <data:DataGrid.Columns>
<data:DataGridTemplateColumn Header="Date"> <data:DataGridTemplateColumn.CellEditingTemplate> <DataTemplate> <TextBlock VerticalAlignment="Top" HorizontalAlignment="Left" Text="{Binding Date}"></TextBlock> </DataTemplate> </data:DataGridTemplateColumn.CellEditingTemplate> </data:DataGridTemplateColumn> <data:DataGridTemplateColumn Header="Original Value"> <data:DataGridTemplateColumn.CellEditingTemplate> <DataTemplate> <TextBlock x:name = "OriginalValue" VerticalAlignment="Top" HorizontalAlignment="Right" Text="{Binding OriginalValue}"></TextBlock> </DataTemplate> </data:DataGridTemplateColumn.CellEditingTemplate> </data:DataGridTemplateColumn> <data:DataGridTemplateColumn Header="Clone Date"> <data:DataGridTemplateColumn.CellEditingTemplate> <DataTemplate> <basics:DatePicker SelectedDateChanged="dateMessageBox" Height="22" VerticalAlignment="Top" DisplayDateStart="10/01/2008" DisplayDateEnd="10/31/2008" SelectedDate="{Binding CloneDate, Mode=TwoWay}"></basics:DatePicker> </DataTemplate> </data:DataGridTemplateColumn.CellEditingTemplate> </data:DataGridTemplateColumn> <data:DataGridTemplateColumn Header="Adjustment Factor"> <data:DataGridTemplateColumn.CellEditingTemplate> <DataTemplate> <TextBox Height="22" x:Name="txtBasicAjustmentFactor" VerticalAlignment="Top" TextAlignment="Right" Width="100" Text="{Binding AdjustmentFactor, Mode=TwoWay}" LostFocus="txtBasicAjustmentFactor_LostFocus" TextChanged="txtBasicAjustmentFactor_TextChanged" /> </DataTemplate> </data:DataGridTemplateColumn.CellEditingTemplate> </data:DataGridTemplateColumn> <data:DataGridTemplateColumn Header="Adjustment Value"> <data:DataGridTemplateColumn.CellEditingTemplate> <DataTemplate> <TextBox x:Name="txtBasicAdjustedValue" VerticalAlignment="Top" Height="22" TextAlignment="Right" Width="100" Text="{Binding AdjustedValue, Mode=TwoWay}" /> </DataTemplate> </data:DataGridTemplateColumn.CellEditingTemplate> </data:DataGridTemplateColumn>
<data:DataGridTemplateColumn Header="Reason Code"> <data:DataGridTemplateColumn.CellEditingTemplate> <DataTemplate> <ComboBox VerticalAlignment="Top" Width="150" Height="22" SelectedIndex="0"> <ComboBoxItem Content="Select Reason" /> <ComboBoxItem Content="A Good Reason" /> <ComboBoxItem Content="A Bad Reason" /> </ComboBox> </DataTemplate> </data:DataGridTemplateColumn.CellEditingTemplate> </data:DataGridTemplateColumn> <data:DataGridTemplateColumn Header="Comments" > <data:DataGridTemplateColumn.CellEditingTemplate > <DataTemplate> <TextBox TextAlignment="Left" VerticalScrollBarVisibility="Auto" MouseEnter="expandComments" MouseLeave="collapseComments" VerticalAlignment="Top" TextWrapping="Wrap" Width="250" Height="22" Text="{Binding Comments, Mode=TwoWay}" /> </DataTemplate> </data:DataGridTemplateColumn.CellEditingTemplate> </data:DataGridTemplateColumn> </data:DataGrid.Columns> </data:DataGrid>Please help me.
Thanks
Deepak
HarshBardhan
Star
10118 Points
1749 Posts
Re: Update one Textbox on textchange of another textbox in DataGrid.
Feb 25, 2009 11:03 AM | LINK
Hi,
Your code is quite big and even i don't have your objects with which you are binding.
So it is difficult me to run the code and comment.
but you can attach text changed event to the text box on whose text changed you want to change text of other text box.
Then you need to get reference of the textbox whose text you want to change.
You can get based on index and then have to change their text.
You can bind another textbox to an observable collection also and can change value of observable collections property with which textbox is bound.
Harsh Bardhan
DeepakGoel
Member
1 Points
4 Posts
Re: Update one Textbox on textchange of another textbox in DataGrid.
Feb 25, 2009 11:11 AM | LINK
{
var dataGridrow = DataGridRow.GetRowContainingElement(sender as FrameworkElement); //Or get clicked index var rowIndex = DataGridRow.GetRowContainingElement(sender as FrameworkElement).GetIndex();//
//Code Rquired Here.
}
using rowindex I ahve to find the textboxes and update the text of second text box. But How...........................
HarshBardhan
Star
10118 Points
1749 Posts
Re: Update one Textbox on textchange of another textbox in DataGrid.
Feb 25, 2009 11:16 AM | LINK
Hi,
Why dont you use observable collection .
As far as seaching based on index is concerened you can check particular row child and can check what is index of your textbox and then change text.
Harsh Bardhan
DeepakGoel
Member
1 Points
4 Posts
Re: Update one Textbox on textchange of another textbox in DataGrid.
Feb 25, 2009 11:46 AM | LINK
hi Harsh,
Thanks for the reply, can you please add some sample code. it would be great help.
yifung
Contributor
3937 Points
637 Posts
Microsoft
Re: Update one Textbox on textchange of another textbox in DataGrid.
Feb 25, 2009 10:37 PM | LINK
You don't need the row index for this. The DataContext of the TextBox is the item your editing. For the calculated fields, you can do this in a couple ways:
DeepakGoel
Member
1 Points
4 Posts
Re: Update one Textbox on textchange of another textbox in DataGrid.
Feb 26, 2009 03:51 AM | LINK
Thanks yifung. Your solution works.
Thanks a lot.
evan.larsen
Member
6 Points
6 Posts
Re: Re: Update one Textbox on textchange of another textbox in DataGrid.
Mar 11, 2011 07:00 PM | LINK
I'm using binding converters that take my item and return the calculated value, but how do I update the converters value upon changing the values in the binding?
Because the value the converter is taking is the entity and its aggregating over its properties and displaying a total. Now this total is not updating itself upon changing the properties in the entity.
How might I accomplish this?