Ondrej Janacek
Ondrej Janacek

Reputation: 12616

Using button event in user control from main window

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?

Upvotes: 1

Views: 2276

Answers (3)

Jason Ridge
Jason Ridge

Reputation: 1868

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

Upvotes: 1

Stephen Holt
Stephen Holt

Reputation: 2368

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.

Upvotes: 1

dcarneiro
dcarneiro

Reputation: 7150

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)

Upvotes: 2

Related Questions