在命令中设置属性时,绑定不会更新

编程入门 行业动态 更新时间:2024-10-21 03:39:57
本文介绍了在命令中设置属性时,绑定不会更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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="schemas.microsoft/winfx/2006/xaml/presentation" xmlns:x="schemas.microsoft/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.

更多推荐

在命令中设置属性时,绑定不会更新

本文发布于:2023-11-11 23:24:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1579824.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   属性   命令

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!