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.

Friend- don't you wantFriend.FirstNameinstead? You need to tell the textbox which property to bind to