How to hookup TextBox's TextChanged event and Command in order to use MVVM pattern in Silverlight

前端 未结 10 2382
一整个雨季
一整个雨季 2020-12-15 18:19

Recently, I realized that MVVM pattern is so useful for Silverlight application and studying how to adopt it into my project.

BTW, how to hook up the textbox\'s text

10条回答
  •  庸人自扰
    2020-12-15 19:02

    In the definition section we add:

    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
    

    If you use the TextBox, add the reference to the event we want to detect:

    
      
                  
          
        
      
    
    

    In the ViewModel add code for Commad:

    public ICommand FilterTextChangedCommand
    {
      get
      {
        if (this._filterTextChangedCommand == null)
        {
          this._filterTextChangedCommand =
            new RelayCommand(param => this.OnRequestFilterTextChanged());
        }
        return this._filterTextChangedCommand;
      }
    }
    
    private void OnRequestFilterTextChanged()
    {
      // Add code
    }
    

    Do not forget to perform the binding text:

    private string _textPrintersFilter;
    public string TextPrintersFilter
    {
      get { return _textPrintersFilter; }
      set
      {
        _textPrintersFilter = value;
        this.RaisePropertyChange(nameof(TextPrintersFilter));
      }
    }
    

提交回复
热议问题