This project is read-only.

this is great, but...

Apr 22, 2010 at 5:54 PM
Edited Apr 22, 2010 at 5:56 PM
As delivered, SimpleModel always returns a constant string so you can't see a change to the value reflected in the view.
Add a button to the SimpleView xaml and make these changes and it will actually do something illustrating the binding.
public class SimpleModel
{
// super easy version
public string SomeSimpleValue { get; set; }
public SimpleModel()
{
SomeSimpleValue = "Some String";
}
public SimpleModel(string value)
{
SomeSimpleValue = value;
}

in SimpleViewModel.cs:

public void UpdateTheValue()
{
SomeSimpleValue = "This has been updated @ " + DateTime.Now.ToString();
}

in SimpleView.xaml:
<Button Content="Change the value" Click="Button_Click"></Button>

in SimpleView.xaml.cs:
private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
{
((SimpleViewModel)this.DataContext).UpdateTheValue();
}

Now it's still very simple, but more informative on adding functionality.
May 5, 2010 at 10:29 PM

Please help.

I followed all the instructions.  My new button is not rendering.  What is wrong?

<UserControl x:Class="SimpleMVVM.View.SimpleView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:Simple="clr-namespace:SimpleMVVM.ViewModel"
    Width="400" Height="300">
    <UserControl.DataContext>
        <Simple:SimpleViewModel />
    </UserControl.DataContext>
    <Grid x:Name="LayoutRoot" Background="White">
        <Button Content="Change the value" Click="Button_Click"></Button>
        <TextBox Text="{Binding SomeSimpleValue, Mode=TwoWay}" />
    </Grid>
</UserControl>

I have cleaned and rebuilt the app.  When I run it there is no button on the browser page.

Why do I have so much trouble with this XAML?

May 5, 2010 at 10:37 PM
Edited May 5, 2010 at 11:04 PM

Okay.  I changed the XAML to define rows.  I need the basics.  (You could have mentioned this step.)  Now I can see the button AND the textbox.

<UserControl x:Class="SimpleMVVM.View.SimpleView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:Simple="clr-namespace:SimpleMVVM.ViewModel"
    Width="400" Height="300">
    <UserControl.DataContext>
        <Simple:SimpleViewModel />
    </UserControl.DataContext>
    <Grid x:Name="LayoutRoot" Background="White">
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>
        <Button Grid.Row="0" Content="Change the value" Click="Button_Click"></Button>
        <TextBox Grid.Row="1" Text="{Binding SomeSimpleValue, Mode=TwoWay}" />
    </Grid>
</UserControl>

But the textbox is not updated by clicking on the button, and I did every step you prescribed.

I found the problem.  The solution includes a hardcoded get in the SimpleModel.  I changed it to return the private property and the demo is working.

Joe

Coordinator
Mar 25, 2012 at 12:14 AM

it appears that your view model is not calling IPropertyNotifyChange otherwise values don't get updated persay.  the sample was the simplesit possible implementation that could be MVVM.  also you should not be calling a function on the vm model as such. the best practice is to just set the value and then it would call its IPropertyNotifyChanged and the change would go around to everyone that needs to know.  calling methods of course if it works your good but for seperation of concerns I try to keep things loosely couples.