Pranit Kothari
Pranit Kothari

Reputation: 9841

Why data binding is not working with PropertyChanged?

I am new to XAML and Windows 8 phone development and learning about data binding. 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.' 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.

Upvotes: 2

Views: 2249

Answers (2)

Liel
Liel

Reputation: 2437

UpdateSourceTrigger=PropertyChanged is not supported in Windows Phone XAML.

You can use UpdateSourceTrigger=Explicit instead, and handle the source updating in the code behind:

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. in that case, the syntax would be:

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

Upvotes: 4

Mario Vernari
Mario Vernari

Reputation: 7304

The UpdateSourceTrigger has nothing to do with the update of the target control. Instead, it is useful when you perform some validation of a TextBox, for instance.

If you have a (view)model behind the XAML code, you should add the INotifyPropertyChanged interface and implement it as the guidelines teach.

Here is an example: http://www.codeproject.com/Articles/41817/Implementing-INotifyPropertyChanged

The XAML snippet should work, at least for WPF. What is not working?

Upvotes: 0

Related Questions