0

テキストボックスとボタンのあるXamlページがあります。ユーザーがボタンをクリックすると、テキストボックスの値がviewModelに渡されます。これを達成する方法は?

4

1 に答える 1

1

xaml:

<TextBox Text={Binding TextBoxAContent} />
<Button Command={Binding ButtonCommand} />

ビューモデルコードは次のようになります。

class MainPageViewModel : ViewModelBase
{
    private string _textBoxAContent;
    public string TextBoxAContent
    {
       get {return _textBoxAContent;}
       set {
              _textBoxAContent = value;
              RaisePropertyChanged("TextBoxAContent");
           } 
    }

    public ICommand ButtonCommand
    {
       get
       {
            return new RelayCommand(ProcessTextHandler);
       }
    }

    private void ProcessTextHandler()
    {
       //add your code here. You can process your textbox`s text using TextBoxAContent property.
    }
}

DataContextまた、ビューコントロールのプロパティを介してビューモデルをビューに割り当てる必要があります。(単にコンストラクターで)

public MainPage()
{
    DataContext = new MainPageViewModel();
}

UPD

ps RelayCommand&ViewModelBase-MVVMLightのクラス

于 2012-11-30T07:13:16.903 に答える