1

I have wpf user control

<StackPanel Orientation="Horizontal">
            <TextBox Name="txbInterval" Text="5"/>
            <Image Name="imgStart" Source="Images/start.png"/>
            <Image Name="imgStop" Source="Images/stop.png"/>
</StackPanel>

I use this control in my application many times. This control can start/stop executing tasks in my own scheduler. When imgStart is clicked, it should create new instance of some task with txbInterval.Text argument. I have this in my MainWindow.xaml

<wp:TaskManager x:Name="tmToolsArMail"/>
<wp:TaskManager x:Name="tmToolsArSail"/>

and I need in Mainwindow.xaml.cs something like this

tmToolsArMail_imgStart_mouseUp(...)
... new MyTask(tmToolsArMail.txbInterval.Text) ...

tmToolsArSail_imgStart_mouseUp(...)
... new MyAnotherTask(tmToolsArSail.txbInterval.Text) ...

How?

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Ondrej Janacek
  • 12,486
  • 14
  • 59
  • 93

3 Answers3

2

IMO, the easiest way to implement this is to create 2 RoutedEvent (Start and Stop) and 1 DependencyProperty (Interval) on your UserControl and then subscribe those events on your parent control (MainWindow)

dcarneiro
  • 7,060
  • 11
  • 51
  • 74
1

What I would do would be to put RoutedEvents in the user control like this:

    public MyUserControl()
    {
        InitializeComponent();

        imgStart.MouseUp += imgStart_MouseUp;
        imgStop.MouseUp += imgStop_MouseUp;
    }

    // Create custom routed events by first registering a RoutedEventID
    // These events use the bubbling routing strategy
    public static readonly RoutedEvent StartEvent = EventManager.RegisterRoutedEvent(
        "Start", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(MyUserControl));

    public static readonly RoutedEvent StopEvent = EventManager.RegisterRoutedEvent(
        "Stop", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(MyUserControl));

    // Provide CLR accessors for the events
    public event RoutedEventHandler Start
    {
        add { AddHandler(StartEvent, value); }
        remove { RemoveHandler(StartEvent, value); }
    }

    // Provide CLR accessors for the events
    public event RoutedEventHandler Stop
    {
        add { AddHandler(StopEvent, value); }
        remove { RemoveHandler(StopEvent, value); }
    }

    // This method raises the Start event
    void RaiseStartEvent()
    {
        RoutedEventArgs newEventArgs = new RoutedEventArgs(MyUserControl.StartEvent);
        RaiseEvent(newEventArgs);
    }

    // This method raises the Stop event
    void RaiseStopEvent()
    {
        RoutedEventArgs newEventArgs = new RoutedEventArgs(MyUserControl.StopEvent);
        RaiseEvent(newEventArgs);
    }

    private void imgStart_MouseUp(object sender, MouseButtonEventArgs e)
    {
        RaiseStartEvent();
    }

    private void imgStop_MouseUp(object sender, MouseButtonEventArgs e)
    {
        RaiseStopEvent();
    }

Then any code which calls into this UserControl can subscribe to those Start and Stop events and do the handling you require.

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Stephen Holt
  • 2,360
  • 4
  • 26
  • 34
  • You could do either, but easiest is from XAML: Then put OnStart and OnStop event handlers in your MainWindow.xaml.cs – Stephen Holt Feb 28 '12 at 12:21
1

I like to implement attached commands, That way if you wanted to do a click style command you can then attach it to any control at a later stage (its all very MVVM).

This is a very nice article on the subject

Here is a Stack Overflow discussion that shows alternatives

Community
  • 1
  • 1
Jason Ridge
  • 1,868
  • 15
  • 27