根据 Silverlight TwoWay 绑定的工作原理,当我更改 FirstName 字段中的数据时,它应该更改 CheckFirstName 字段中的值。
为什么不是这样?
回答:
谢谢杰夫,对其他人来说就是这样:这是带有可下载代码的完整解决方案。
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;
}