Different between binding and x:bind

hxy 46 Reputation points
2019-12-31T05:07:22.617+00:00

What is the Different between binding and x:bind in UWP.
I do not know how to choose in my app.
please reply me soon. thanks

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

Accepted answer
  1. Fay Wang - MSFT 5,191 Reputation points
    2019-12-31T06:22:27.777+00:00

    Hello,

    ​Welcome to Microsoft Q&A!

    First, the DataContext of x:bind is current page, it will find the property in the current Code-Bebind class. But for Binding, assumes, by default, that you're binding to the DataContext of your markup page, so you need to set DataContext explicitly for Binding.

    And the default mode of x:bind is oneTime, it means it will change the value one time. So in general, you need to set the mode as oneWay which is the default mode of Binding. In addition, x:bind generates source code at compile-time, the binding will take effect at runtime, so x:bind is more faster. For more details about their difference, you can refer to this document.

    Update:

    First, I create a simple ViewModel which contains a Text property.

    public class MyViewModel : INotifyPropertyChanged  
        {  
            public event PropertyChangedEventHandler PropertyChanged = delegate { };  
            public string text { get; set; }  
            public string Text {  
                get {   
                    return text;   
                }  
                set {  
                    text = value;  
                    OnPropertyChanged();  
                }  
            }  
      
            public MyViewModel() {  
                this.Text = "first";  
            }  
      
            public void OnPropertyChanged([CallerMemberName] string propertyName = null)  
            {  
                // Raise the PropertyChanged event, passing the name of the property whose value has changed.  
                this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));  
            }  
        }  
    

    For x:bind:

    We need to set the mode to OneWay and don't need to explicitly specify its context.

    .xaml:

    < TextBlock Text="{x:Bind VM.Text,Mode=OneWay}">  
    

    .cs:

    public MainPage()  
        {  
            this.InitializeComponent();  
      
            VM = new MyViewModel();  
        }  
             
    public MyViewModel VM { get; set; }  
    

    For Binding:

    We don't need to set the mode to OneWay since its default mode is OneWay and we need to explicitly specify its context.

    .xaml:

    < TextBlock Text="{Binding VM.Text}">  
    

    .cs:

    public MainPage()  
        {  
            this.InitializeComponent();  
      
            VM = new MyViewModel();  
            this.DataContext = this;  // it means current context is this page.  
        }  
    public MyViewModel VM { get; set; }  
    

    When you want to reassign Text:

    private void Button_Click(object sender, RoutedEventArgs e)  
        {  
            VM.Text = "Hello";  
        }  
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful