Binding doesn't update when property is set inside a command

本小妞迷上赌 提交于 2019-12-24 20:12:50

问题


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>

回答1:


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.




回答2:


You missed the statement ViewModelBase:INotifyPropertyChanged on ViewModelBase



来源:https://stackoverflow.com/questions/19689447/binding-doesnt-update-when-property-is-set-inside-a-command

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!