WPF: Custom routed event with extra information

I have a normal WPF window, let's call it TopLevel which has a bunch of controls in it, among other things a simple ListView which should log certain events in different elements in the application.

So suppose I have a Grid in TopLevel which contains a bunch of user controls called Task. Each Task has an object associated with them as a public property, let's call it Order, as well as a standard checkbox.

Now I want TopLevel to receive an event whenever the user checks a checkbox in a Task, but the event should contain the Order object as well, so I can work with it from TopLevel to put it into the event log.

How do I do that? I'm guessing I want to use routed events for it, but I can't figure out how to get the checkbox click to "find" Order to send it upwards to TopLevel.

--------------Solutions-------------

How about something like this...

private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
CheckBox checkBox = sender as CheckBox;
Task task = FindParentTask(checkBox);
Order order = task.Order;
}

Since you need to traverse up the visual tree to get to Task, you could try a bit of recursion...

public FrameworkElement FindParentTask(FrameworkElement element)
{
if (element.Parent.GetType() == typeof(Task))
return element.Parent as FrameworkElement;
else
return FindParentTask(element.Parent as FrameworkElement);
}

I've just tested this method to retrieve the parent Expander for a CheckBox on one of my UserControls, it's several levels up the visual tree, nested in a heap of StackPanels, Grids and DockPanels, worked a treat.

Category:c# Time:2010-01-22 Views:1

Related post

  • WPF Custom Routed event question 2008-11-10

    How do you get two unrelated controls to raise the same custom event? All examples I have seen so far have an event defined within a single control, should I be taking a different approach? Eg. I'd like to raise a custom bubbling event from an OnFocu

  • WPF XAML Intellisense doesn't work correctly for custom routed event handlers 2009-12-07

    I defined a custom routed event with the following constructs (names changed): public class MyRoutedEventArgs : RoutedEventArgs {...} public delegate void MyRoutedEventHandler(Object sender, MyRoutedEventArgs args); public static readonly RoutedEvent

  • Custom Routed Event Fails! 2009-12-19

    My application looks like this: 1 Window 2 Frame 3 Page 4 Control In Control (4) I have a custom routed event, which i want Frame (2) to handle. The event: public static readonly RoutedEvent PreviewArtistLinkClickedEvent = EventManager.RegisterRouted

  • How can I raise a custom Routed Event from user control? 2012-04-13

    In my user control I have a button that, when clicked, would raise a custom Routed Event. I've attempted to raise it, but it doesn't get fired in the MainWindow.xaml. Xaml for the button in UserControl: <Button x:Name="PART_Add" Content="+" Grid.C

  • Interactivity.EventTrigger not subscribing to Custom Routed Event 2012-04-25

    I have a custom behavior inside a DataTemplate, which raises a custom Routed Event. I want to handle that event with a TriggerAction (send a message, invoke a command, etc). <Border Background="#01FFFFFF" VerticalAlignment="Center"> <i:Inter

  • adding custom routed event of user control to xaml of a window in wpf 2010-09-01

    I am little new to Command binding so this might be a trivial question to many. I know that we can add Command bindings in xaml of a window and give its correspondng property in viewmodel. This viewmodel will be given to the DataContext of the window

  • Error when using WPF Custom Control Event 2012-03-07

    Hopefully this isn't too confusing. All this is in regards to WPF, C# 4.0. Using Visual Studio 2010 Professional. In my program, I have some method Update();. This method ends up accessing a DLL (QuantLib, which uses PInvoke fwiw) that I created. In

  • WPF and routed event 2009-07-27

    I have a WPF window, which has a button that is inside a stackPanel, which is inside another stackPanel I wrote an event handler for the button for the MouseDown event. I want to execute this eventHandler three times for the button and the parent (st

  • WPF MVVM Routed Events 2009-10-18

    There are a lot of discussions about M-V-VM and Command binding (RelayCommand) but not a lot has been covered on Routed Events binding to a handler in a M-V-VM pattern. I want to find what is the best approach. Here is an example of RoutedEvent bindi

  • How can I raise a custom Routed Event from a DataTemplate? 2012-04-14

    I have a User Control,called dCB_Props that contains several objects, most importantly a ComboBox that's bound to an Observable Collection. Though the collection can take any object, it will normally take a UserControl called EditDeleteItem. I've set

  • Wpf stop routing event when MessageBox appear? 2010-12-29

    I've PreviewMouseDown event on TreeView in order to determine if user can select other item based on some logic. If the current item data changed, will appear MessageBox that asks the user if he want to discard the changes. if user press YES , I set

  • Custom attached events in WPF 2009-07-13

    I might be getting the terminology wrong here, but I think I'm trying to create an attached event. In the Surface SDK, you can do things like: <Grid Background="{StaticResource WindowBackground}" x:Name="Foo" s:SurfaceFrameworkElement.ContactChang

  • WPF - Handling custom attached events on custom controls 2009-08-27

    I have a routed event declared as such (names have been changed to protect the innocent): public class DragHelper : DependencyObject { public static readonly RoutedEvent DragCompleteEvent = EventManager.RegisterRoutedEvent( "DragComplete", RoutingStr

  • In a WPF custom control, is it possible for a child element to Template bind to the event of the control? 2010-10-28

    I have a custom control that contains two elements that can be clicked (a button and a check box). I'd like to be able to put events in the XAML for each of these events. i.e. <Control OnButtonClick="SomeEvent" OnCheckBoxClick="SomeOtherEvent" /

  • wpf working with events 2011-12-05

    I wrote my own control with some buttons and events - that works like a charm. Then I dynamically put this controls as a child in StackPanel in other class. How could I in this class (with StackPanel) get events from my custom control- I have a publi

  • WPF: Routed events or regular events? 2009-05-03

    In WPF we have routed events. When should these be used instead of regular events? --------------Solutions------------- Routed events have special behavior, but that behavior is largely invisible if you are handling an event on the element where it i

  • passing Event Arguments of original handler to Routed Event in wpf 2010-12-21

    The following code shows a normal event and a routed event. Here I have used the same event name for explaining purposes but in reality I am using just the routed event. //Normal Event public event SelectedHandler Selected; public delegate void Selec

  • How to distinguish between Sender and Source in WPF routed events 2011-05-31

    I would like to have a clear explanation about how to determine in each situation which control is the sender and which one is the source for a WPF Routed Event event both in the case of tunnelling and bubbling events. Edit: Suppose you have an event

  • How to modify a property of a received WPF routed event arg parameter before resuming its propagation? 2011-08-25

    I'm playing with a custom TextBox inheriting from the WPF TextBox, trying to learn about WPF events, so my problem is the following: When the TextBox receives an input, I want it to receive instead a case inverted version of this input. For example,

Copyright (C) pcaskme.com, All Rights Reserved.

processed in 0.467 (s). 13 q(s)