x:bind - notify object change, after button is clicked

BitSmithy 1,751 Reputation points
2024-03-20T12:08:09.7633333+00:00

Hello,

I have a problem with x:bind, UI isnt notified when object is created.

I have such XAML:

        <TextBox Text="{x:Bind myClass.Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBox>

        <Button Content="TEST" Click="Button_Click"></Button>
    </StackPanel>

c#

namespace XBIND_test
{
    public sealed partial class MainPage : Page
    {
        public MyClass myClass;

        public MainPage()
        {
            this.InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            myClass = new MyClass();
        }
    }

    public class MyClass : INotifyPropertyChanged
    {
        private string name = "AAAA";
        public string Name
        {
            get { return name; }
            set
            {
                name = value;
                PropertyChanged(this, new PropertyChangedEventArgs("Name"));
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
    }
}

When UI is initialized myClass object is null. It will be created when user clicks button. After button is clicked, "AAAA" string should appear in the TextBox. How to code such functionality using x:bind?

Universal Windows Platform (UWP)
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 112.5K Reputation points
    2024-03-20T15:47:32.98+00:00

    Try an additional notification:

    public sealed partial class MainPage : Page, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        . . .
        private void Button_Click( object sender, RoutedEventArgs e )
        {
            myClass = new MyClass( );
            PropertyChanged( this, new PropertyChangedEventArgs( nameof( myClass ) ) );
        }
    }
    
    0 comments No comments

0 additional answers

Sort by: Most helpful