user3292642
user3292642

Reputation: 761

WPF MVVM Data Binding

Im trying to implement the MVVM Pattern i just want to have a TextBox that shows some initial text at startup.

this is my view: (dont care about the buttons and the listbox for now)

<Window x:Class="Friends.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>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <TextBox Grid.Row="0" Width="150" Text="{Binding Friend}"></TextBox>
    <ListBox Grid.Row="1" Width="150"></ListBox>
    <Button Grid.Row="2" Content="Previous" Width="150"></Button>
    <Button Grid.Row="3" Content="Next" Width="150"></Button>
</Grid>

this is my model:

public class FriendsModel : INotifyPropertyChanged
{
    private string _firstName;
    public string FirstName
    {
        get { return _firstName; }
        set
        {
            _firstName = value;
            RaisePropertyChanged("FirstName");
        }
    }
    public FriendsModel(string _initialName)
    {
        _firstName = _initialName;
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public void RaisePropertyChanged(string _newName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) {
            handler(this, new PropertyChangedEventArgs(_newName));
        }
    }
}

and this is my viewmodel:

public class FriendsViewModel
{
    public FriendsModel Friend { get; set; }
    public FriendsViewModel()
    {
        Friend = new FriendsModel("Paul");
    }

}

in the code behind i have:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = new FriendsViewModel();
    }
}

my project is building without any errors but it doesnt show the text in my textbox. Can anyone help me?

thanks in advance

edit:

i changed it to

<TextBox Grid.Row="0" Width="150" Text="{Binding Friend.Firstname}"></TextBox>

its still not working.

Upvotes: 0

Views: 337

Answers (4)

sexta13
sexta13

Reputation: 1568

Have you tried this:

 public FriendsModel(string _initialName)
    {
        this.FirstName = _initialName;
    }

Regards,

Upvotes: 0

Andy
Andy

Reputation: 6466

The DataContext is being set right after InitializeComponent() is called, this means that the bindings have already been setup, the textbox is correctly binding to the FirstName property but at the point of binding it's empty.

if you want the textbox to update when the property does you'll need to set DataContext before InitializeComponent()

 public MainWindow()
        {    
            DataContext = new FriendsViewModel();
            InitializeComponent();      
        }

gives the result

enter image description here

Upvotes: 0

user3540473
user3540473

Reputation: 11

the Friend in the binding represents the full object, you must specify the membre...

try to replace{Binding Friend} by {Binding Friend.FirstName}

Upvotes: 1

Dmitry
Dmitry

Reputation: 2052

The binding should point the FirstName property. WPF can not figure out by him self how to convert Friend class to string.

Text="{Binding Friend.FirstName}"

Upvotes: 2

Related Questions