【发布时间】:2010-10-07 21:36:05
【问题描述】:
根据 Silverlight TwoWay 绑定的工作原理,当我更改 FirstName 字段中的数据时,它应该更改 CheckFirstName 字段中的值。
为什么不是这样?
答案:
谢谢杰夫,就是这样,其他人:这里是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>
后面的代码:
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;
}
【问题讨论】:
标签: silverlight data-binding two-way-binding