Question

I am having a surprising difficulty trying to make a simple thing work, that is, setting a property in a method called by a Command bound to a Button.

When I set the property in the ViewModel constructor, the correct value is properly displayed in View, but when I set this property with the command's method, the View doesn't update, although any breakpoint I create is reached (even inside RaisePropertyChanged in my ViewModelBase). I am using vanilla RelayCommand found easily in online tutorials (from Josh Smith if I am not mistaken).

My project can be downloaded here (Dropbox);

Some important code blocks are below:

ViewModel:

public class IdiomaViewModel : ViewModelBase
{

    public String Idioma {
        get { return _idioma; }
        set { 
            _idioma = value;
            RaisePropertyChanged(() => Idioma);
        }
    }
    String _idioma;



    public IdiomaViewModel() {
        Idioma = "nenhum";
    }


    public void Portugues () { 
        Idioma = "portu";
    }
    private bool PodePortugues()
    {
        if (true) // <-- incluir teste aqui!!!
            return true;
        return false;
    }
    RelayCommand _comando_portugues;
    public ICommand ComandoPortugues {
        get {
            if (_comando_portugues == null) {
                _comando_portugues = new RelayCommand(param => Portugues(),
                                                param => PodePortugues());
            }
            return _comando_portugues;
        }
    }



    public void Ingles () { 
        Idioma = "ingle";
    }
    private bool PodeIngles()
    {
        if (true) // <-- incluir teste aqui!!!
            return true;
        return false;
    }
    RelayCommand _comando_ingles;
    public ICommand ComandoIngles {
        get {
            if (_comando_ingles == null) {
                _comando_ingles = new RelayCommand(param => Ingles(),
                                                param => PodeIngles());
            }
            return _comando_ingles;
        }
    }

}

View with no extra code behind:

<Window x:Class="TemQueFuncionar.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:app="clr-namespace:TemQueFuncionar"
        Title="MainWindow" Height="350" Width="525">

    <Window.DataContext>
        <app:IdiomaViewModel/>
    </Window.DataContext>

    <StackPanel>
        <Button Content="Ingles" Command="{Binding ComandoIngles, Mode=OneWay}"/>
        <Button Content="Portugues" Command="{Binding ComandoPortugues, Mode=OneWay}"/>
        <Label Content="{Binding Idioma}"/>

    </StackPanel>
</Window>
Was it helpful?

Solution

Youdid fill the Interface implementation put you did not mention it to the base view model. You are missing this : INotifyPropertyChanged linking Interface to the base class, this makes the the View refreshes the content.

OTHER TIPS

You missed the statement ViewModelBase:INotifyPropertyChanged on ViewModelBase

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top