使用wpf和Devexpress MVVM设置文本框文本

时间:2017-08-25 01:51:43

标签: c# wpf mvvm devexpress devexpress-wpf

如何在点击按钮时使用wpf和Devexpress免费MVVM设置Text的{​​{1}}?

这是我的文本框的xaml代码

Textbox

这是我的VM代码

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

代码为执行但不会更改 public virtual string SelectedText { get; set; } void AutoUpdateCommandExecute() { Console.WriteLine("Code is Executing"); SelectedText = "TextBox Text is Not Changing"; } 的{​​{1}}

但是当我在Text中输入内容并使用此代码获取该文本框的文本时。

Textbox

它打印我在文本框中输入的文本。那我做错了什么?我无法设置文本?

谢谢。

1 个答案:

答案 0 :(得分:1)

您选择的文本需要是DependencyProperty,然后通知绑定它的值已更改...

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

您的VM需要实现INotifyPropertyChanged接口,而在SelectedText设置器中,您需要触发属性更改事件...

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