Why is two-way binding in silverlight not working?
Posted
by Edward Tanguay
on Stack Overflow
See other posts from Stack Overflow
or by Edward Tanguay
Published on 2009-02-16T14:51:05Z
Indexed on
2010/06/15
9:52 UTC
Read the original article
Hit count: 348
According to how Silverlight TwoWay binding works, when I change the data in the FirstName field, it should change the value in CheckFirstName field.
Why is this not the case?
ANSWER:
Thank you Jeff, that was it, for others: here is the full solution with downloadable code.
XAML:
<StackPanel>
<Grid x:Name="GridCustomerDetails">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="300"/>
</Grid.ColumnDefinitions>
<TextBlock VerticalAlignment="Center" Margin="10" Grid.Row="0" Grid.Column="0">First Name:</TextBlock>
<TextBox Margin="10" Grid.Row="0" Grid.Column="1" Text="{Binding FirstName, Mode=TwoWay}"/>
<TextBlock VerticalAlignment="Center" Margin="10" Grid.Row="1" Grid.Column="0">Last Name:</TextBlock>
<TextBox Margin="10" Grid.Row="1" Grid.Column="1" Text="{Binding LastName}"/>
<TextBlock VerticalAlignment="Center" Margin="10" Grid.Row="2" Grid.Column="0">Address:</TextBlock>
<TextBox Margin="10" Grid.Row="2" Grid.Column="1" Text="{Binding Address}"/>
</Grid>
<Border Background="Tan" Margin="10">
<TextBlock x:Name="CheckFirstName"/>
</Border>
</StackPanel>
Code behind:
public Page()
{
InitializeComponent();
Customer customer = new Customer();
customer.FirstName = "Jim";
customer.LastName = "Taylor";
customer.Address = "72384 South Northern Blvd.";
GridCustomerDetails.DataContext = customer;
Customer customerOutput = (Customer)GridCustomerDetails.DataContext;
CheckFirstName.Text = customer.FirstName;
}
© Stack Overflow or respective owner