Hi I am working on a MVVM Silverlight app and i use UpdateSourceTrigger Property of TextBox to update the bindingexpression in runtime as follows:
.xaml.cs:
BindingExpression beID = txtEmpID.GetBindingExpression(TextBox.TextProperty); beID.UpdateSource();
BindingExpression beAge = txtAge.GetBindingExpression(TextBox.TextProperty); beAge.UpdateSource();
.xaml:
<Grid x:Name="LayoutRoot"
Background="White"
DataContext="{Binding Source={StaticResource keyEMPVM},
UpdateSourceTrigger=Explicit}">
//<Grid.RowDefinitions>
//<Grid.ColumnDefinitions>
<sdk:Label Grid.Row="2"
Grid.Column="1"
Target="{Binding ElementName=txtEmpID}" />
<TextBox x:Name="txtEmpID"
Grid.Row="2"
Grid.Column="2"
Style="{StaticResource ContentTextBoxStyle}"
Text="{Binding emp.ID, Mode=TwoWay, ValidatesOnExceptions=True,
ValidatesOnDataErrors=True, NotifyOnValidationError=True, UpdateSourceTrigger=Explicit}" />
<sdk:Label Grid.Row="4"
Grid.Column="1"
Target="{Binding ElementName=txtAge}" />
<TextBox x:Name="txtAge"
Grid.Row="4"
Grid.Column="2"
Style="{StaticResource ContentTextBoxStyle}"
Text="{Binding emp.Age, Mode=TwoWay, ValidatesOnExceptions=True,
ValidatesOnDataErrors=True, NotifyOnValidationError=True, UpdateSourceTrigger=Explicit}" />
<Button x:Name="btnSubmit"
Grid.Row="6"
Grid.Column="1"
Grid.ColumnSpan="2"
Style="{StaticResource ContentButtonStyle}"
Content="Submit"
Command="{Binding Path=UpdateEmployee}"
CommandParameter="{Binding emp.ID}" />
In this case i m doing the bindingexpressions manually for each textbox.. Is there a way by which i can do this bindingexpressions for all the textbox controls inside the grid at a single instance.
If you want to update each
TextBox
in a Grid, you can use this code:Where KeyDown event is just an example. Perhaps recursion isn't the best way to solve the problem, but it's the easiest way