MVVM Light RaisePropertyChanged剂量不起作用

本文关键字:不起作用 RaisePropertyChanged Light MVVM | 更新日期: 2023-09-27 18:08:34

Model:

 namespace PlanszaGlowna.Model
 {
     public class PlanszaGlowna
     {
         public bool CzyWidoczny { get; set; }

在ViewModel

namespace PlanszaGlowna.WidokModelu
{
    public class PlanszaGlowna : ViewModelBase
    {
        private Model.PlanszaGlowna m_PlanszaGlowna;
        public bool CzyWidoczny
        {
            get { return m_PlanszaGlowna.CzyWidoczny; }
            set
            {
                m_PlanszaGlowna.CzyWidoczny = value;
                RaisePropertyChanged(() => CzyWidoczny);
            }
        }

和那个绑定:

         <UserControl x:Class="PlanszaGlowna.Widok.PlanszaGlowna" 
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         Visibility="{Binding Path=CzyWidoczny, Converter={StaticResource Bool2VisibilityKonwerter}, Mode=TwoWay}"
当我在ViewModel中为CzyWidoczny设置新值时,UI不更新。我使用MVVM Light

MVVM Light RaisePropertyChanged剂量不起作用

问题是模型类PlanszaGlowna.Model.PlanszaGlowna没有实现INotifiyPropertyChanged

所以实现相同并更改为

 public class PlanszaGlowna : INotifiyPropertyChanged
 {
    ...
    private _CzyWidoczny;
    public bool CzyWidoczny
    {
        get { return _CzyWidoczny; }
        set
        {
            _CzyWidoczny = value;
            RaisePropertyChanged("CzyWidoczny");
        }
    }

只要你的类PlanszaGlowna.Model.PlanszaGlowna实现了INotifiyPropertyChanged并且你已经创建了包装器方法RaisePropertyChanged来调用PropertyChanged事件

如果你在你的绑定中尝试这样做:

 <UserControl x:Class="PlanszaGlowna.Widok.PlanszaGlowna" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     Visibility="{Binding Path=CzyWidoczny, Converter={StaticResource Bool2VisibilityKonwerter}, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

添加UpdateSourceTrigger=PropertyChanged来绑定。也修改你的属性已经创建了下一个结构:

private string _text= "";
public string Text
    {
        get { return _text; }
        set { _text= value; RaisePropertyChanged("Text"); }
    }    

On in case Text属性属于一个类,它有下一个结构:

public class NewClass: INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    private string _text;
    public string Text
    {
        get { return _text; }
        set
        {
            if ( _text!= value)
            {
                _text= value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("Text"));
                }
            }
        }
    } 
}