简体   繁体   English

Button.Text 的绑定不起作用 Xamarin.Forms

[英]Binding for Button.Text does not work Xamarin.Forms

I'm trying to bind a Button.Text property to a C# property using data binding but the view does not update, if I change the value of the C# Property.我正在尝试使用数据绑定将 Button.Text 属性绑定到 C# 属性,但如果我更改 C# 属性的值,则视图不会更新。

Here are some Code snippets:以下是一些代码片段:

    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:viewmodel="clr-namespace:Xamarin_App.ViewModel"
             x:Class="Xamarin_Lernen.MainPage">

    <ContentPage.BindingContext>
        <viewmodel:MainViewModel/>
    </ContentPage.BindingContext>
    
    <StackLayout>

        <Button Text="{Binding Text}" Command="{Binding Command}"/>

In the XAML is just the Button inside a StackLayout and I set the DataContext在 XAML 中只是 StackLayout 中的按钮,我设置了 DataContext

View Model:查看 Model:

private string _Text;

        public string Text
        {
            get => _Text;

            set
            {
                if (_Text == value)
                {
                    return;
                }

                _Text = value;
                OnPropertyChanged();
            }
        }

public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

    public ICommand Command { get; }

public MainViewModel()
{
    Command = new NewCommand();
}

Here is the property, the Property changed event and the command for the button这是属性、属性更改事件和按钮的命令

MainViewModel mainVM = new MainViewModel();

            mainVM.Text = "Test";

and that is what the button command does.这就是按钮命令的作用。

Where is the problem and how to fix it?问题出在哪里以及如何解决?

You need to assign the bindingcontext of the XAML file to the mainviewmodel type object mainvm instead of creating a new mainviewmodel class object,such as:您需要将 XAML 文件的 bindingcontext 分配给 mainviewmodel 类型 object mainvm 而不是创建新的 mainviewmodel class ZA8CFDE63131BD59EB68 as

viewModel = (MainViewModel)this.BindingContext;

The key is that you need add a method to the Command and then update the Text Property for the button.关键是您需要向命令添加一个方法,然后更新按钮的Text属性。 You can refer to sample snippets below:您可以参考以下示例片段:

ViewModel:视图模型:


 public class MainPageViewModel : INotifyPropertyChanged
    {
        
      public event PropertyChangedEventHandler PropertyChanged;

      private string _Text;
      public ICommand Command { get; set; }
      public string Text
        {
            get { return _Text; }
            set {
                if(_Text != value) { _Text = value; }
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Text"));

           }
        }


       public MainPageViewModel()
        {
            Command = new Command(mytext);
        }


       private void mytext(object obj)
        {
           //update the Text Property
           Text = "This is a test";
        }
    }

Xaml: Xaml:

<Button Text="{Binding Text}" Command="{Binding Command}"></Button>

Last but not least, don't forget to bind the view with viewmodel in Code-behind below or in Xaml.最后但同样重要的是,不要忘记在下面的代码隐藏或 Xaml 中将视图与视图模型绑定。

BindingContext = new MainPageViewModel();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM