Search code examples
c#.netwpf.net-4.0routed-events

Routed Events & ScrollViewer


Here's how my element tree is composed (irrelevant informations removed).

<TabItem Style="Click_PutNormalCursorBack">
   <ScrollViewer>
      <ItemsControl Style="ContainsMuchLabels_n_CollapsibleGroupeHeaders">
         <Label Style="Click_ChangeCursor" />
      </ItemsControl>
   <ScrollViewer>
</TabItem>

I am simulating Drag n Drop. When you click on the clickable label, it transform's your cursor to guive you the impression your dragging it. What i want is that clicking on the TabItem, it put back your normal cursor.

I cant use PreviewMouseDown event because it would never allow the label to be clicked.

One solution i found was adding a big panel to my element tree between my ScrollViewer and my ItemsControl with a transparent background to recieve clicks, putting an event handler on my ItemsControl's CollapsibleGroupHeader and ItemsPanel but its not really an elegant solution.

I tough RoutedEvents in WPF where ther to resolve that kind of problems...

Note : my handlers are not my my styles, styles where just ther for guiving more information

Is ther a better solution than mine?


Solution

  • Use the MouseDown event for both. Just don't mark the event handled (e.Handled = true) if the event doesn't apply. For example, in your clickable tab, if it's clicked when the cursor isn't in the drag state, don't mark handled. Once the cursor IS in the drag state, if the user clicks again on the clickable label, don't mark handled (unless you want that event to reset the cursor). You could also use the PreviewMouseDown event on the tab, but again don't mark handled unless that click is from a cursor that originates from the label.

    If you don't mark an event handled it continues to propagate so multiple controls can handle it. Even if something does mark an event handled and you still want to handle it, you can make an event fire even when marked handled: see "Adding Instance Handlers That Are Raised Even When Events Are Marked Handled" in this article