-1

I am trying to make the two way binding work on Textblock with compilation binding x:Bind

Problem: Even though setting TwoWay bind mode and PropertyChanged source trigger. Can not get it to work. When the object property balance changed in code behind it does not get updated in UI.

Below is the code.

XAML

<TextBlock  x:Name="Balance"
            Text="{x:Bind classObject.Balance,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

Code-Behind

private ClassName classObject = new ClassName() { Name = "Foo",Balance = 100 };
public Observable ViewModel { get; } = new Observable();
private void NavLinksList_ItemClick(object sender,ItemClickEventArgs e)
{
    classObject = new ClassName() { Name = "Blah",Balance = 10 * new Random().NextDouble() * (50 - 1) + 1 };
}

Observable

public class Observable : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected void Set<T>(ref T storage, T value, [CallerMemberName]string propertyName = null)
    {
        if (Equals(storage, value))
        {
            return;
        }

        storage = value;
        OnPropertyChanged(propertyName);
    }

    protected void OnPropertyChanged(string propertyName) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

Alternate solution

Set Textblock.Text in code behind manually.

But the issue is property changed event is supposed to work and automatically update the text block without explicit coding.

I searched other questions , but couldnt find the similar one.

Morse
  • 8,258
  • 7
  • 39
  • 64
  • What does "not work" mean here? What do you expected to happen and when? – mm8 Jul 18 '18 at 13:58
  • >>"But the issue is property changed event is supposed to work and automatically update the text block without explicit coding." – Morse Jul 18 '18 at 14:01
  • class property is same, so I think it should. dont it? – Morse Jul 18 '18 at 14:04
  • Whoever downvoted the question, please explain the downvote, downvoting without feedback wont help improve the posts. – Morse Jul 18 '18 at 15:08

1 Answers1

1

If you want to update the data-bound Text property of the TextBlock you should set the source property that it's bound to, i.e. the Balance property of classObject. Also note that the ClassName class should implement the INotifyPropertyChanged interface and raise change notifications.

Setting the classObject field to a new instance of a ClassName is not supposed to update the TextBlock, unless you call Bindings.Update() afterwards.

mm8
  • 163,881
  • 10
  • 57
  • 88
  • I removed setting it to new instance code , but it still does not work and `Observable` class has implemented `INotifyProperyChanged` which is in turn made as `ViewModel` property. I have used `ObservableCollection` in similar fashion , that works though. – Morse Jul 18 '18 at 14:10
  • You are not binding the TextBlock to the Observable so why would this matter? You are binding to className.Balance, aren't you? Why are you even creating an Onservable? You don't seem to use it somewhere anyway. – mm8 Jul 18 '18 at 14:11
  • How do I bind TextBlock to Observable, I thought making it as class property is enought since I have getter set to new instance of ViewModel i,e Observable. Do I need to declare ClassName object in Observable class? – Morse Jul 18 '18 at 14:15
  • What property of the Observable do you want to bind the TextBlock to...!? The Observable class has no properties. – mm8 Jul 18 '18 at 14:16
  • all the properties of `className` object. Calling `Bindings.Update()` solved the issue. Thanks a lot! – Morse Jul 18 '18 at 14:19