code-zoop
code-zoop

Reputation: 7368

Getting the Binding object in the hyperlinkbutton - click event?

I have a ListBox with a binding to an ObservableCollection of type MyViewModel. The item template is a HyperLinkButton. Is there a way to pass the MyViewModel to the HyperLinkButton.Click - event? The event has 2 parameters, object sender and RoutedEventArgs. The sender is of type HyperLinkButton and I does not have any reference to the Binding object at all! Is there a way to add MyViewModel as a parameter to the click event?

Thanks

Upvotes: 0

Views: 1151

Answers (1)

Rik van den Berg
Rik van den Berg

Reputation: 2839

The Property CommandParameter should do the job.

<HyperLinkButton Click="ClickEvent" CommandParameter={Binding} />

This should work because it will use the current MyViewModel which is binded to it. Then you simply need to access it in the event args. There is also an alternative. You can obtain the DataContext from the sender which is the HyperLinkButton. Which directs you to the MyViewModel instance. Hope this helps

Edit: Seems kinda wierd you dont have an object reference in your HyperLinkButton. U sure it is not the datacontext you need from it?

Upvotes: 3

Related Questions