为什么 silverlight 中的双向绑定不起作用?
根据 Silverlight TwoWay 绑定的工作原理,当我更改 FirstName 字段中的数据时,它应该更改 CheckFirstName 字段中的值。
为什么情况并非如此?
回答:
谢谢 Jeff,就是这样,对于其他人来说:这里是 带有可下载代码的完整解决方案。
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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的
Customer
类型必须支持INotifyPropertyChanged
以便绑定知道您的FirstName
属性值何时发生更改。此教程可能会帮助您将代码工作。
Your
Customer
type has to supportINotifyPropertyChanged
in order for the binding to know when yourFirstName
property value has changed.This tutorial may help you in getting your code to work.
网格容器内的控件不知道 FirstName、LastName 和 Address 是什么。 我认为您需要将网格绑定到代码隐藏中的对象:
现在该容器内的每个控件都可以绑定到 Customer 的属性。 您可以像这样绑定它:
在后面的代码中,确保“Customer”是一个类对象,并且是公开声明的。
如果这不起作用,请尝试将 x:Name="" 添加到顶部的页面声明和命名空间。
我希望这有帮助!
Your controls inside of the Grid container have no idea what FirstName, LastName, and Address are. I think that you need to bind your grid to the object in the codebehind:
Now every control inside of that container can be bound to a property of Customer. You bind it like so:
In your code behind, make sure that "Customer" is a class object, and is declared publicly.
If this doesn't work, try adding an x:Name="" to the page declarations and namespaces at the top.
I hope this helps!
解决方案是使用
CheckFirstName
的元素绑定solution is use element binding for
CheckFirstName