具有链接的复杂类的 MVVM 属性和 CanExecute Relay Command 不起作用

发布于 2024-10-04 03:59:00 字数 1500 浏览 0 评论 0原文

我在 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似乎没有注意到更改并且未启用,使其无法使用...

是否可以将 CanExecute 与这种 set-tu 一起使用

I have a entity class in a C# library class and linked to Silverlight class library
(entities must be in C# class because of legacy compatiblity with other systems)

Example (C# library):

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

This class is linked to Silverlight class library.

On a MVVM there is a property

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

The property is binded to a control in XAML

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

I want to control the button with RelayCommands CanExecute depended on the TestValue in TestClass...

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

However, if the TestValue in changed (different then empty string), PressCommand CanExecute doen't seem to notice the change and is not enabled, making it unusable...

Is it possible to use the CanExecute with this kind of set-tu

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笔芯 2024-10-11 03:59:00

您需要做的是当值更改时调用 PressCommand.CanExecuteChanged()。 中值的属性更改

要做到这一点,需要监听属性VM

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

What you need to do is call PressCommand.CanExecuteChanged() when the value changes. To do this nicely listen for the property change of the value in the property

VM

 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();
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文