2017-08-25 8 views
0

ボタンをクリックしたときにwpfとDevexpressフリーMVVMを使用してTextTextboxを設定するにはどうすればよいですか?wpfとDevexpress MVVMを使用してテキストボックステキストを設定

これは、これは私のVMコード

 public virtual string SelectedText { get; set; } 
     void AutoUpdateCommandExecute() 
     { 
      Console.WriteLine("Code is Executing"); 
      SelectedText = "TextBox Text is Not Changing"; 
     } 

コードが実行されるが、それはTextbox

Textを変更しないで、私のテキストボックスのXAMLコード

<TextBox Width="200" Grid.Column="1" Text="{Binding SelectedText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" FontSize="14"/> 

ですしかし、私はtextboxに何かを入力し、このコードを使用してそのテキストボックスのテキストを取得します。

 void AutoUpdateCommandExecute() 
     { 
      Console.WriteLine("Code is Executing"); 
      Console.WriteLine(SelectedText); 
     } 

テキストボックスに入力したテキストを印刷します。それで私は何が間違っていたのですか?私はテキストを設定することはできませんか?

ありがとうございます。

答えて

1

あなたの選択したテキストは、それが価値だとの結合は、あなたのVMがINotifyPropertyChangedのインターフェイスを実装する必要があり、SelectedTextに...

public static readonly DependencyProperty SelectedTextProperty = DependencyProperty.Register("SelectedText", typeof(string), typeof(YourClassType)); 

public string SelectedText 
{ 
    get { return (string)GetValue(SelectedTextProperty); } 
    set { SetValue(SelectedTextProperty , value); } 
} 

OR

を変更したことを通知しますたDependencyPropertyのいずれかである必要がありますあなたがプロパティ変更されたイベントを発生させる必要があります。

public class VM : INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 
    private string _selectedText; 
    public string SelectedText 
    { 
    get { return _selectedText; } 
    set 
    { 
     _selectedText = value; 
     RaisePropertyChanged("SelectedText"); 
    } 
    } 

    private void RaisePropertyChanged(string propertyName) 
    { 
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 
    } 
} 
+0

ありがとうございました。私は2番目を使いました。しかし、私の質問はここに何が置かれているのですか? YourClassType **** typeof(YourClassType) –

+1

DependencyPropertyはクラスが定義されているクラスに属しているため、クラスがClassExampleと呼ばれる場合、DependencyPropertyレジスタにはtypeof(ClassExample)が設定されます。これは、フレームワークにどのクラスがプロパティを見つけるかを指示します。 – CodexNZ

+0

ありがとう。あなたの説明が本当に助けてくれた –

関連する問題