tags:

views:

22

answers:

2

Code Sample:

<DataTemplate x:Key="NodeDataTemplate">
    <Border Style="{StaticResource nodeBorderStyle}"
                        MouseEnter="SetMouseCursor_Arrow"
                        MouseLeave="SetMouseCursor_ScrollAll"
                        MouseLeftButtonDown="ViewLink"
                        MouseLeftButtonDown="SetFlagForCursorTracking">
....
</DataTemplate>

I want to add 2 handlers to a particular event like shown above. However this won't compile - 'attribute can be set more than once'. I have multiple methods because they do different things (are named appropriately). e.g. the first handler has nothing in common with the second.

My other option was to kludge a SetFlagForCursorTrackingAndCheckForViewLink method - which is "Yech!". Any ideas ?

+1  A: 

Please try to just add one handler which will subsequently call some event handling method. Or add just one handler which will subsequently call your desired two event handling methods - this will be more elegant.

Piotr Justyna
I'd favor the kludge because there would be one less method to find an acceptable name for i.e. the delegator. My whole drive is for readability and maintainability.. e.g. if someone one wants to fix a bug for cursor tracking.. it should be visible somehow from the XAML Datatemplate... unless I name the delegator with the name I chose for the kludge.. unless there is a better answer - I guess you'd get the accepted mark.
Gishu
The code Akash presented is not best imo - it will work of course, but this is a perfect example of tight binding and blocks you the way to easily perform changes on your xaml. Try to look at my answer from this point of view: if an event rises within your view, only one method (or command) in your code behind (or respectively VM) will be invoked to perform some further actions (call other handling methods). It's perfectly clear and elegant solution. Please tell me if you have any further concerns.
Piotr Justyna
BTW why to name it delegator? :) Far less confusing would be On[UIElementName]_[UIEvent] (OnMyButton_Click) for an event handling method in CB - it will perform further actions. The same thing should concern VM, but insted of [UIElementName] I would suggest using [UIElementFunctionality] - e.g. SearchUsersCommand/ShowUsersCommand/HideUsersCommand.
Piotr Justyna
@Piotr - I wasn't going to name it delegator :) but that is what it does.. In which case its name ideally should combine all the stuff that it's going to trigger off
Gishu
In no case imo :) Name should only reflect the fact that this method is actually handling a specified event/action - remember about avoiding tight binding. This is the body of this method that should contain appripriate actions.
Piotr Justyna
A: 

You can do that in code behind,

UIObject.AddHandler( AttachedEventContainer.AttachedEvent, your_handler);

UIObject.AddHandler( AttachedEventContainer.AttachedEvent, your_handler);

I have not tried or tested this, if this gives any error then you have no choice but use a method and call two event handlers manually in that method and attached method as event handler.

Akash Kava
The event publisher is embedded in a (potentially sharable) Datatemplate. To add code-behind I'd have to add a code-behind for the ResourceDictionary.. workable.. but too much work. I'd use the kludge approach since this is the only roadblock for now.
Gishu
I think you want to do something for visual effect in your MouseLeftButtonDown, in that case, why dont you derive and make a custom control, keep your visual effect code in your custom code and only "ViewLink" business logic can be in the datatemplate.
Akash Kava