根据 Silverlight TwoWay 绑定(bind)的工作原理,当我更改 FirstName 字段中的数据时,它应该更改 CheckFirstName 字段中的值。
为什么不是这样?
回答:
谢谢 Jeff,就是这样,对于其他人:这里是 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;
}
最佳答案
您的 Customer
类型必须支持 INotifyPropertyChanged
为了让绑定(bind)知道您的 FirstName
属性值何时更改。
这tutorial可能会帮助您使代码正常工作。
关于silverlight - 为什么 silverlight 中的双向绑定(bind)不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/553521/