<Label Content="Surname"/>
<TextBox Text="{Binding CustomerToEdit.Surname}"/>
<Button Content="Apply Changes"
Command="{Binding ApplyChangesCommand}" />
</StackPanel>
このコードは、のforenameとの2つのTextBox
esでされるなデータフォームをし
TextBox 。TextBoxにLabelがあり、フォームのに「Apply」 Buttonがあります。
のTextBoxをしてTextプロパティをます
Text="{Binding CustomerToEdit.Forename}"
TextBoxのテキストをにするのではなく、このなは、テキストを "path" CustomerToEdit.Forenameに バインドします。このなパスはですかビューの「データコンテキスト」 - このはビューモデルで す。バインディング・パスは、ビュー・モデルのCustomerToEditプロパティです。これはCustomerタイ プのもので、 Forenameというプロパティーをします。したがって、「ドットき」パスです。
に、 ButtonのXAMLをると、ビューモデルのApplyChangesCommandプロパティにバインドされた
Commandがあります。これは、ボタンをVMのコマンドにするためになことです。
}
private void DataGrid_CollectionChanged(object sender,
System.Collections.Specialized.NotifyCollectionChangedEventArgs e) {
//Do what ever }
MVVMでじことをするNoは、
Commandsをします<Button Command="{Binding Path=CmdStartExecution}" Content="Start" />
コマンドプロパティにはあるのプレフィックス Cmd
をすることをおめします。なぜな
ら、にxamlでそれらをとするからです。それはMVVMですので、あなたはそのコマンドのためにしたいButton 「EQ」 Button_Clickあなた に ViewModel 。
そのためには、に2つのことがです。
System.Windows.Input.Command 1.
RelayCommandたとえば、 ここからします 。 2.
なはのようになります。
private RelayCommand _commandStart;
public ICommand CmdStartExecution {
get {
if(_commandStart == null) {
_commandStart = new RelayCommand(param => Start(), param => CanStart());
}
return _commandStart;
} }
public void Start() {
//Do what ever }
public bool CanStart() {
return (DateTime.Now.DayOfWeek == DayOfWeek.Monday); //Can only click that button on mondays.
}
それでは、これはにはですか
ICommandは、xamlのControlがバインドしているものです。 RelayCommandはあなたのコマンドを
ActionルーティングしますつまりMethodびします。 Null-Checkは、Commandがパフォーマンスのの
ためにしかされないようにします。のRelayCommandのリンクをんだら、 RelayCommand
2つのオーバ
ーロードがあることにづいたかもしれません。 (Action<object> execute)および(Action<object>execute, Predicate<object> canExecute) 。
それはあなたがaditionallyすることができますMethodすboolすることをえるためにControl
wheather
「イベント」は、やすることはできません。
そのためのいことは、 Methodがfalseをす、えばButtonがEnabled="false"なることです
CommandParameters
<DataGrid x:Name="TicketsDataGrid">
<DataGrid.InputBindings>
<MouseBinding Gesture="LeftDoubleClick"
Command="{Binding CmdTicketClick}"
CommandParameter="{Binding ElementName=TicketsDataGrid, Path=SelectedItem}" />
</DataGrid.InputBindings>
<DataGrid />
このでは、 DataGrid.SelectedItemをのViewModelのClick_Commandにしたいとし
DataGrid.SelectedItem 。
ICommandのはのようになりますが、メソッドはこのようになります。
private RelayCommand _commandTicketClick;
public ICommand CmdTicketClick {
get {
if(_commandTicketClick == null) {
_commandTicketClick = new RelayCommand(param => HandleUserClick(param));
}
return _commandTicketClick;
} }
private void HandleUserClick(object item) {
MyModelClass selectedItem = item as MyModelClass;
if (selectedItem != null) {
//Do sth. with that item }
}
オンラインでWPFのMVVMをむ https://riptutorial.com/ja/wpf/topic/2134/wpfのmvvm