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}" />

TestClassのTestValueに依存するRelayCommands CanExecuteでボタンを制御したい...

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

ただし、TestValue が変更された場合 (空の文字列とは異なります)、PressCommand CanExecute は変更に気付かないようで、有効になっていないため、使用できません...

この種のset-tuでCanExecuteを使用することは可能ですか

4

1 に答える 1

1

あなたがする必要があるのは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();
 }
于 2010-11-25T08:54:43.753 に答える