tags:

views:

186

answers:

1

How can bind an event on a WPF Control to a method on my ViewModel?

I have a ViewModel:

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

In a DataTemplate I've got:

<TextBlock Text="{Binding Text}">

Now I would like to attach a method on my ViewModel to the TextBlock, something like:

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

I cannot figure out how to do this without creating a callback in the Code-behind.

+1  A: 

Look into using AttachedCommandBehavior from here. It allows you to bind Commands to events entirely in XMAL. Not exactly what you want, but it will give you the same outcome.

Martin Harris
Not my favorite solution, but it works.
Hallgrim