DingDing
DingDing

Reputation: 38

MVVMLight: Xaml don't refresh when the viewmodel is updated

I'm implementing a WP8.1 project with MVVMlight. In my project, I have multiple ViewModels (one by features).

When My IHM is loading, the binding with the ViewModels works fine.

But When i click on IHM button in order to refresh my Viewmodel (i use a RelayCommand), my IHM don't refresh my fields according to my ViewModels.

Xaml code:

 <Page
x:Class="ApplicationMobileWinPhone.IHM.Test"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:ApplicationMobileWinPhone.IHM"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
 DataContext="{Binding Main, Source={StaticResource Locator}}">
<Grid>
    <TextBox Foreground="Black" x:Name="TextAdress" Text="{Binding mytest.A}"  Margin="0,40,0,0" ></TextBox>
    <Button Command="{Binding mytest.SearchCommand,  Mode=OnWay}" Content="OK"
            CommandParameter="{Binding Text, ElementName=TextAdress}"
            BorderThickness="0" MinHeight="39" MinWidth="39" x:Name="ButtonSearch" VerticalAlignment="Bottom" Height="50" Width="32" Margin="358,-9,0,593">

    </Button>
</Grid>

ViewModels Implementation:

public class Mytest : ViewModelBase
{
    private RelayCommand<string> _searchCommand;
    private readonly INavigationService _navigationService;
    public string A
    {
        get;
        set;
    }
    public Mytest(INavigationService nav) 
    {
        _navigationService = nav;
        A = "init";
    }
    public RelayCommand<string> SearchCommand
    {
        get
        {
            return _searchCommand
                   ?? (_searchCommand = new RelayCommand<string>(
                        a =>
                       {
                           A = "modify";
                       }
             ));
        }
    }
}

I don't understand because there is an other ViewModels and it works fine.

Upvotes: 0

Views: 184

Answers (1)

Mark
Mark

Reputation: 8291

You will have to call RaisePropertyChanged, you will not have to implement it. Simply add the following line after assigning a new value to the field:

...
A = "modify";
RaisePropertyChanged(() => A);
...

Upvotes: 1

Related Questions