【问题标题】:MVVM property with a linked complex class and CanExecute Relay Command not working具有链接的复杂类和 CanExecute 中继命令的 MVVM 属性不起作用
【发布时间】:2010-11-25 08:28:00
【问题描述】:

我在 C# 库类中有一个实体类并链接到 Silverlight 类库 (实体必须在 C# 类中,因为与其他系统的遗留兼容性)

示例(C# 库):

public class TestClass
{        
    private string _testValue;
    public string TestValue
    {
        get { return _testValue; }
        set
        {
            if (_testValue!= value)
            {
                _testValue = value;
                OnPropertyChanged("TestValue");
            }
        }
    }}

这个类链接到 Silverlight 类库。

在 MVVM 上有一个属性

private TestClass _testProp = new TestClass();
        public TestClass TestProp
        {
            get
            {
                return _testProp ;
            }
            set
            {
                if (value != _testProp )
                {
                    _testProp = value;
                    RaisePropertyChanged("TestProp");
                    PressCommand.CanExecuteChanged();
                }
            }
        }

属性绑定到 XAML 中的控件

<TextBox Text="{Binding TestProp.TestValue, Mode=TwoWay}">
<Button Content="Press" Command="{Binding PressCommand}" />

我想用 RelayCommands CanExecute 控制按钮取决于 TestClass 中的 TestValue...

    PressCommand = new RelayCommand(() =>
    {
        DoSomething();
    }, () => TestProp.TestValue != string.empty);

但是,如果 TestValue 发生更改(不同于空字符串),PressCommand CanExecute 似乎没有注意到更改并且未启用,使其无法使用...

这种set-tu可以用CanExecute吗

【问题讨论】:

    标签: silverlight mvvm mvvm-light relaycommand canexecute


    【解决方案1】:

    您需要做的是在值更改时调用PressCommand.CanExecuteChanged()。为此,请很好地监听属性中值的属性变化

    虚拟机

     public TestClass TestProp
        {
            get
            {
                return _testProp ;
            }
            set
            {
                if (value != _testProp )
                {
                    if(_testProp != null)
                    {
                        _testProp.PropertyChanged -= TestPropChanged;
                    }
    
    
                    _testProp = value;
    
                    if(_testProp != null)
                    {
                        _testProp.PropertyChanged += TestPropChanged;
                    }
    
                    RaisePropertyChanged("TestProp");
                    PressCommand.CanExecuteChanged();
                }
            }
        }
    
     private void TestPropChanged(object sender, PropertyChangedEventArgs e)
     {
          //Can check for specific property if required...
    
          PressCommand.CanExecuteChanged();
     }
    

    【讨论】:

    • 谢谢!你的灵魂/漫游效果很好!顺便说一句,GalaSoft MVVM 是 RaiseCanexecuteChanged(),而不是 CanExecuteChanged(),我的错,对不起任何阅读...
    • 此解决方案存在一个问题。它不适用于 Silverlight Toolkit NumericUpDown 控件...看来,NumericUpDown 控件值在“检查”后更新。
    • @KolarJ 也许问另一个关于 NumericUpDown 的问题,因为我没有玩过那么多
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-11
    • 2014-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多