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

こ雲淡風輕ζ 提交于 2019-12-05 07:13:38
  • in your model you have the textMessage as being an int rather than string?

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

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

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

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

Where MyMessage should be a public property of MainViewModel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!