user3065219
user3065219

Reputation: 45

Unable to bind data to textbox using MVVM architecture?

I am new to MVVM and WPF. I have tried to to bind data to a textbox using DataContext.

Model: MyMessage.cs

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));
        }
    }

ViewModel: MainViewModel.cs

class MainViewModel
{
    MyMessage myMessage;
    public MainViewModel()
    {
        myMessage = new MyMessage();
        myMessage.TestMessage="Hai";
    }

View : MainWindow.xaml

<Window x:Class="DemoApp2.Views.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <TextBox HorizontalAlignment="Left" Height="23" Margin="10,10,0,0" TextWrapping="Wrap" Text="{Binding TestMessage}" VerticalAlignment="Top" Width="120"/>
</Grid>

Upvotes: 0

Views: 249

Answers (2)

rmnblm
rmnblm

Reputation: 157

Try this:

class MainViewModel
{
    private MyMessage _messageProperty;
    public MyMessage MessageProperty 
    {
            get { return _messageProperty; }
            set { _messageProperty = value; }
    }

    public MainViewModel()
    {
        _messageProperty = new MyMessage();
        _messageProperty.TestMessage="Hai";
    }

Also the string in your OnPropertyChanged event must be the same name as the property, like this:

public string TestMessage
{
    get { return testMessage; }
    set
    {
        testMessage = value;
        OnPropertyChanged("TestMessage");
    }
}

In the code-behind file of your MainWindow.xaml.cs, set the data context to your ViewModel:

class MainWindow
{
    public MainWindow()
    {
        this.DataContext = new MainViewModel();
    }

And in your MainWindow.xaml file, you have to refer to the nested property of your MessageProperty

<Window x:Class="DemoApp2.Views.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <TextBox HorizontalAlignment="Left" Height="23" Margin="10,10,0,0" TextWrapping="Wrap" Text="{Binding MessageProperty.TestMessage}" VerticalAlignment="Top" Width="120"/>
</Grid>

Let me know if it worked and if you need more informations ;-) Also I recommend you to make a Quickstart Tutorial of how MVVM works and how it's implemented e.g. http://www.codeproject.com/Articles/165368/WPF-MVVM-Quick-Start-Tutorial

Upvotes: 1

Erti-Chris Eelmaa
Erti-Chris Eelmaa

Reputation: 26268

You need to turn "myMessage" into property, and bind it as MyMessage.TestMessage in your TextBox, assuming you bind MainViewModel as DataContext in Window.

Upvotes: 4

Related Questions