wpf 如何使用 MVVM 和 MVVM 工具包将属性绑定到文本框?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9193221/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 23:17:26  来源:igfitidea点击:

How To Bind a Property to Textbox using MVVM and MVVM toolkit?

wpfmvvmmvvm-toolkit

提问by niknowj

I am new to MVVM. to learn I created a sample application to show a message in a text box while clicking on button. In my code the button command is working properly but the property is not binding to the Textbox. How to bind Property to Textbox using MVVM?

我是 MVVM 的新手。为了学习,我创建了一个示例应用程序,用于在单击按钮时在文本框中显示一条消息。在我的代码中,按钮命令工作正常,但该属性未绑定到文本框。如何使用 MVVM 将属性绑定到文本框?

My code is similar like given below.

我的代码类似于下面给出的代码。

View

看法

<TextBox Name="MessageTextBox" Text="{Binding TestMessage}"/>
<Button Content="Show" Name="button1" Command="{Binding ShowCommand}">
 <!-- Command Handler -->
</Button>

View Model

查看模型

MyMessage myMessage; 
public MainViewModel()
{
myMessage=new MyMessage();
}

//inside the ShowCommand Handler

TestMessage="Hello World";

// A Property to set TextBox Value. 

Model

模型

public class MyMessage: INotifyPropertyChanged        
{     
    private string testMessage;
    public string TestMessage
    {
        get { return testMessage; }
        set
        { 
            testMessage= value;
            OnPropertyChanged("TestName");
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

}

回答by emybob

  • in your model you have the textMessage as being an int rather than string?
  • 在您的模型中,您将 textMessage 作为 int 而不是字符串?

try something like this:

尝试这样的事情:

VIEWMODEL

视图模型

 private MyMessage message;

 public MainViewModel()
 {
    message = new MyMessage();
 }

public MyMessage Message
{
    get { return message;}
    set { message = value;}
}

//in your command: 
this.Message.TestMessage = "Hello World!";

MODEL

模型

public class MyMessage: INotifyPropertyChanged
{
   private string testMessage

   public string TestMessage;
   { 
      get{ return testMessage; }
      set
         { 
           testMessage = value; 
           this.OnPropertyChanged("TestMessage");
         } 
   }
     //INotifyChanged Events   
}

XAML

XAML

<TextBox Text="{Binding Message.TestMessage, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

回答by Nagg

I don't understand your code but I guess you should fix your binding with this:

我不明白你的代码,但我想你应该用这个修复你的绑定:

<TextBox Name="MessageTextBox" Text="{Binding MyMessage.TestMessage}"/>

Where MyMessageshould be a public property of MainViewModel

哪里MyMessage应该是公共财产MainViewModel