user6189573
user6189573

Reputation: 43

How to use read-only Expression Body Properties with x:Bind in UWP apps

I would like to declare read-only real-time properties using expression body:

    public string RealTimeData => RetrievLiveData(Duration);

Use x:Bind to update UI controls:

 <TextBlock x:Name="LiveDataCtrl" Text="{x:Bind RealTimeData, Mode=OneWay}"/>

The binding updates when the page loads, but I can't figure out a way to update the binding from the page (e.g., from a button click).

Being it's not dependency property and not backed by INPC, I have been looking for ways to trigger the binding manually.

I've tried GetBindingExpression with no luck:

    LiveDataCtrl.GetBindingExpression(WhatGoesHere?);

Upvotes: 0

Views: 231

Answers (1)

Xie Steven
Xie Steven

Reputation: 8591

The GetBindingExpression method returns the BindingExpression object which contains information about a single instance of a Binding, not x:Bind.

There's no bindingexpression with x:Bind. But in your case, you could call this.Bindings.Update(); to force it to update source.

For example:

public sealed partial class MyUserControl1 : UserControl
{
    public string RealTimeData => RetrievLiveData();
    public MyUserControl1()
    {
        this.InitializeComponent();
    }

    private string RetrievLiveData()
    {
        return DateTime.Now.ToString();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        this.Bindings.Update();
    }
}

Upvotes: 1

Related Questions