Reputation: 892
i am beginner in mvvm concept.so i tried one sample application.it contains two textbox are name and id, one submit button,one label.when i click submit button it combine the two strings from the textbox and display in label.
i can submit the values and in viewmodel the property contain the result.but its not shown in view.why..?
in view contains
<grid>
<TextBox Grid.Column="1" Grid.Row="1" Text="{Binding name}" Height="23" Margin="9" Name="txtname" Width="120" />
<TextBox Grid.Column="1" Grid.Row="2" Text="{Binding id}" Height="23" Margin="9" Name="txtid" Width="120" />
<Button Command="{Binding submit}" Content="submit" Grid.Column="2" Grid.Row="2" Height="23" Name="btnsubmit" Width="75" />
<Label Content="{Binding display}" Grid.Row="3" Height="38" HorizontalAlignment="Left" Name="lbldisplay" Width="192" />
</grid>
view.cs code is
public MainWindow()
{
InitializeComponent();
DataContext = new DemoViewModel();
}
in my viewmodel contains two .cs file
one is DemoViewModelInotify.cs.in this i write the code for inotifypropertychanged. another one is DemoViewModel.cs.this contain the property and commands.
namespace mvvmdemonixon.ViewModel
{
public class DemoViewModel :DemoViewModelInotify
{
public ICommand submit { get; set; }
public string name { get; set; }
public string display { get; set; }
public int id{get;set;}
public DemoModel model { get; set; }
public DemoViewModel()
{
submit = new DelegateCommand<object>(this.add);
}
public void add(object paramter)
{
string a= mvvmdemonixon.Model.DemoModel.addstring(name, id);
display = a;
}
}
}
my model contains
namespace mvvmdemonixon.Model
{
public class DemoModel
{
public static string addstring(string name1, int no1)
{
string display = "The Student Name Is " + name1 + "and Id Is" + no1 + ".";
return display;
}
}
}
in my app.xaml.cs
private void OnStartup(object sender, StartupEventArgs e)
{
mvvmdemonixon.MainWindow view = new MainWindow();
view.DataContext = new mvvmdemonixon.ViewModel.DemoViewModel();
view.Show();
}
in my app xaml
<Application x:Class="mvvmdemonixon.App"
Startup="OnStartup">
</Application>
advance thanks..
Upvotes: 0
Views: 2405
Reputation: 5715
There are two things you were missing. The first thing is your View's binding is OneTime. The second thing is you ViewModel does not notify when a property changed.
To fix the first problem, you have to update xaml like below.
<Label Content="{Binding display, Mode=OneWay}" Grid.Row="3" Height="38" HorizontalAlignment="Left" Name="lbldisplay" Width="192" />
To fix your second problem, you have to implement INotifyPropertyChanged.
Upvotes: 0
Reputation: 37790
WPF binding engine uses INotifyPropertyChanged
(for scalar properties) and INotifyCollectionChanged
(for collection properties) interfaces to reflect changes, which has been made in bound data source (view model).
This:
public string display { get; set; }
means, that any property setting will not notify the view, because setter's code doesn't contain any notifications. The code should be like this:
public string display
{
get { return _display; }
set
{
if (_display != value)
{
_display = value;
OnPropertyChanged("display");
}
}
}
private string _display;
where OnPropertyChanged
raises INotifyPropertyChanged.PropertyChanged
event.
This is true for other view-bound properties in your view model, which should update the view.
Upvotes: 2
Reputation: 7123
You need to implement INotifyPropertyChanged in your ViewModel. And raise the property changed event from each property setter.
Upvotes: 0