MVVM and Silverlight help
I am trying to learn the MVVM pattern using Silverlight. There are tons of videos 开发者_开发知识库and blogs. I understand it in a high level, but cant seem to get my own implementation of it.
I got the following View:
<UserControl x:Class="SilverlightApplication1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="clr-namespace:SilverlightApplication1.ViewModel"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="White">
<TextBlock Text="{Binding Name, Mode=TwoWay}" Width="200" Height="200"/>
<Button Name="btn1" Width="200" Height="20" Margin="100,268,100,12" Click="btn1_Click"/>
</Grid> </UserControl>
In my VM, I have: namespace SilverlightApplication1.ViewModel
{
public class ViewModel : INotifyPropertyChanged
{
private Model.UserModel m_model;
public ViewModel()
{
m_model = new Model.UserModel();
}
public string Name
{
get
{
return m_model.Name;
}
set
{
if (value != m_model.Name)
{
m_model.Name = value;
InvokePropertyChanged("Name");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void InvokePropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
and in my Model:
namespace SilverlightApplication1.Model
{
public class UserModel
{
public string Name { get; set; }
}
}
Basically, I want to simulate a button click and update the property so that it can fire the property changed event. I tried hard coding the property on the MainPage code behind like this:
private void btn1_Click(object sender, RoutedEventArgs e)
{
ViewModel.ViewModel vm = new ViewModel.ViewModel();
vm.Name = "Test";
}
Shouldnt this update the property (for Name) and raise the propertychanged event? I have seen other examples do something similar to this. I dont understand as to what is subscribing to the event
Can anyone shine some light?
In this example, to fix it, try this code:
private void btn1_Click(object sender, RoutedEventArgs e)
{
ViewModel.ViewModel vm = new ViewModel.ViewModel();
this.LayoutRoot.DataContext = vm;
vm.Name = "Test";
}
Note: I know you just learning MVVM, but after you're comfortable with this paradigm, you should adopt a framework so you can focus on things besides the MVVM plumbing. In saying that, doing things in the code-behind is not the MVVM way, like setting the data context. However, I assume you're just experimenting and learning, so it's fine for now.
I think the step you're missing here is setting the WPF form's datacontext to an instance of your viewmodel.In your xaml set your UserControl datacontext to a new viewmodel like below:
<UserControl.DataContext>
<vm:ViewModel />
</UserControl.DataContext>
Then just do normal sets in your vm and it should bubble up the changes to your binding.
精彩评论