在命令中设置属性时,绑定不会更新 [英] Binding doesn't update when property is set inside a command

查看:126
本文介绍了在命令中设置属性时,绑定不会更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个令人惊讶的困难,试图使一个简单的事情工作,即设置一个属性在一个方法调用一个命令绑定到一个按钮。

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.

当我在ViewModel构造函数中设置属性时,正确的值在View中正确显示,但是当我使用命令的方法设置此属性时,尽管我创建的任何断点(甚至在 RaisePropertyChanged 在我的 ViewModelBase 中)。我正在使用在线教程(来自乔希·史密斯,如果我没有误会)容易找到的香草 RelayCommand

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).

我的项目可以下载 here (Dropbox);

My project can be downloaded here (Dropbox);

一些重要的代码块如下:

Some important code blocks are below:

ViewModel:

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;
        }
    }

}

没有额外的代码:

<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>


推荐答案

Youdid填写接口实现,你没有提到到基本视图模型。
你错过了这个:INotifyPropertyChanged 链接到基类的接口,这使得View刷新内容。

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.

这篇关于在命令中设置属性时,绑定不会更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆