简体   繁体   English

为什么数据绑定不能与PropertyChanged一起使用?

[英]Why data binding is not working with PropertyChanged?

I am new to XAML and Windows 8 phone development and learning about data binding. 我是XAML和Windows 8电话开发的新手,并且了解数据绑定。 Here , it is suggested that I need to use, 在这里 ,建议我需要使用,

UpdateSourceTrigger=PropertyChanged

But when I try to use it in my xaml, it is giving error as 'Requested value 'PropertyChanged' not found.' 但是,当我尝试在xaml中使用它时,由于“未找到请求的值'PropertyChanged'”,因此出现错误。 Instead it is working correctly with, 相反,它可以正常工作,

UpdateSourceTrigger=Default

I am doing something wrong, or it is deprecated in newer versions. 我做错了事,或者在较新的版本中不推荐使用。

My code sample, 我的代码示例

 <TextBox x:Name="txt1" Height="100" Width="100"></TextBox>

 <TextBlock Grid.Row="1" x:Name="txt2" Height="100" Width="100" 
    Text="{Binding Text,ElementName=txt1,
 UpdateSourceTrigger=PropertyChanged}"></TextBlock>

Thanks. 谢谢。

UpdateSourceTrigger=PropertyChanged is not supported in Windows Phone XAML. Windows Phone XAML不支持UpdateSourceTrigger=PropertyChanged

You can use UpdateSourceTrigger=Explicit instead, and handle the source updating in the code behind: 您可以改用UpdateSourceTrigger=Explicit ,并在后面的代码中处理源代码更新:

private void OnTextBoxTextChanged( object sender, TextChangedEventArgs e )
{
  TextBox textBox = sender as TextBox;
  BindingExpression bindingExpr = textBox.GetBindingExpression( TextBox.TextProperty );
  bindingExpr.UpdateSource();
}

Another alternative would be to use Coding4Fun's library BindingHelper. 另一种选择是使用Coding4Fun的库 BindingHelper。 in that case, the syntax would be: 在这种情况下,语法为:

<TextBox   
  Text="{Binding FooBar, Mode=TwoWay}" 
  local:TextBinding.UpdateSourceOnChange="True" />

The UpdateSourceTrigger has nothing to do with the update of the target control. UpdateSourceTrigger与目标控件的更新无关。 Instead, it is useful when you perform some validation of a TextBox, for instance. 而是,例如,当您对TextBox执行一些验证时,它很有用。

If you have a (view)model behind the XAML code, you should add the INotifyPropertyChanged interface and implement it as the guidelines teach. 如果您在XAML代码的后面有一个(视图)模型,则应添加INotifyPropertyChanged接口并按照指南中的说明实施。

Here is an example: http://www.codeproject.com/Articles/41817/Implementing-INotifyPropertyChanged 这是一个示例: http : //www.codeproject.com/Articles/41817/Implementing-INotifyPropertyChanged

The XAML snippet should work, at least for WPF. XAML代码段应该至少对WPF有效。 What is not working? 什么不起作用?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM