2009-09-01 14 views
2

WPFコントロールのイベントをViewModelのメソッドにバインドする方法はありますか?ViewModelのメソッドをWPFのイベントに添付する

私はViewModelに持っている:

<TextBlock Text="{Binding Text}"> 

今私は、TextBlockのに私のViewModelのメソッドを添付するような何か希望:

を私は持っているのDataTemplateで

class MyViewModel { 
    public string MyText { get; set; } 
    public void MyMouseHandleMethod(object sender, EventArgs e) { } 
} 

<TextBlock Text="{Binding MyText}" MouseUp="{Binding MyMouseHandleMethod}"> 

コードビハインドでコールバックを作成せずにこれを行う方法を理解できません。

答えて

3

hereからAttachedCommandBehaviorを使用して検索してください。コマンドをXMAL内のイベントに完全にバインドすることができます。あなたが望むものではありませんが、それはあなたに同じ結果を与えるでしょう。

+0

私のお気に入りの解決策ではありませんが、機能します。 – Hallgrim

1

私はイベントでカスタムマークアップ拡張を使用して.NET 4.5以降で動作する新しいソリューションを用意しました。それは、など、引数の値を提供するために、複数の引数、バインディングおよびその他の拡張機能で使用することができます私はここで完全な詳細にそれを説明する:

http://www.singulink.com/CodeIndex/post/building-the-ultimate-wpf-event-method-binding-extension

使用法:

<!-- Basic usage --> 
<Button Click="{data:MethodBinding OpenFromFile}" Content="Open" /> 

<!-- Pass in a binding as a method argument --> 
<Button Click="{data:MethodBinding Save, {Binding CurrentItem}}" Content="Save" /> 

<!-- Another example of a binding, but this time to a property on another element --> 
<ComboBox x:Name="ExistingItems" ItemsSource="{Binding ExistingItems}" /> 
<Button Click="{data:MethodBinding Edit, {Binding SelectedItem, ElementName=ExistingItems}}" /> 

<!-- Pass in a hard-coded method argument, XAML string automatically converted to the proper type --> 
<ToggleButton Checked="{data:MethodBinding SetWebServiceState, True}" 
       Content="Web Service" 
       Unchecked="{data:MethodBinding SetWebServiceState, False}" /> 

<!-- Pass in sender, and match method signature automatically --> 
<Canvas PreviewMouseDown="{data:MethodBinding SetCurrentElement, {data:EventSender}, ThrowOnMethodMissing=False}"> 
    <controls:DesignerElementTypeA /> 
    <controls:DesignerElementTypeB /> 
    <controls:DesignerElementTypeC /> 
</Canvas> 

    <!-- Pass in EventArgs --> 
<Canvas MouseDown="{data:MethodBinding StartDrawing, {data:EventArgs}}" 
     MouseMove="{data:MethodBinding AddDrawingPoint, {data:EventArgs}}" 
     MouseUp="{data:MethodBinding EndDrawing, {data:EventArgs}}" /> 

<!-- Support binding to methods further in a property path --> 
<Button Content="SaveDocument" Click="{data:MethodBinding CurrentDocument.DocumentService.Save, {Binding CurrentDocument}}" /> 

ビューモデルのメソッドのシグネチャ:

public void OpenFromFile(); 
public void Save(DocumentModel model); 
public void Edit(DocumentModel model); 

public void SetWebServiceState(bool state); 

public void SetCurrentElement(DesignerElementTypeA element); 
public void SetCurrentElement(DesignerElementTypeB element); 
public void SetCurrentElement(DesignerElementTypeC element); 

public void StartDrawing(MouseEventArgs e); 
public void AddDrawingPoint(MouseEventArgs e); 
public void EndDrawing(MouseEventArgs e); 

public class Document 
{ 
    // Fetches the document service for handling this document 
    public DocumentService DocumentService { get; } 
} 

public class DocumentService 
{ 
    public void Save(Document document); 
} 
関連する問題