简体   繁体   中英

Custom Events from User Control to Parent Control in WPF

I have a simple TabControl inside my UserControl which is embedded in the parent control like this:

 <StackPanel>       
        <myviews:myusercontrol></myviews:myusercontrol>                      
    </StackPanel>

Inside myusercontrol I have the following implementation:

  public partial class MyUserControl
    {
        public delegate void SelectedTabItemHandler(string selectedTabItem);
        public event SelectedTabItemHandler TabItemSelected;

        public MyUserControl()
        {
            InitializeComponent();
        }     

        private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if(TabItemSelected != null)
            {
                TabItemSelected("some selected Item");
            }
        }
    }

How can I hook up the TabItemSelected event in my XAML like this:

<myviews:myusercontrol TabItemSelected="TabSelectionChanged"></myviews:myusercontrol>  

UPDATE 2:

For passing custom parameters I am doing something like this:

 public partial class PatientEditTabView : UserControl
    {       
        // Routed Event 
        public static readonly RoutedEvent SelectedTabItemEvent = EventManager.RegisterRoutedEvent("TabItemSelected", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(PatientEditTabView));

        public PatientEditTabView()
        {
            InitializeComponent();
        }     

        public event RoutedEventHandler TabItemSelected
        {
            add { AddHandler(SelectedTabItemEvent, value);  }
            remove { RemoveHandler(SelectedTabItemEvent, value); }
        }

        private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            SelectedTabRoutedEventArgs eventArgs = new SelectedTabRoutedEventArgs(PatientEditTabView.SelectedTabItemEvent,tabControl.SelectedValue as String);
            RaiseEvent(eventArgs);
        }
    }

UPDATE 3:

 public class SelectedTabRoutedEventArgs : RoutedEventArgs
    {
        private readonly String _selectedItem; 

        public SelectedTabRoutedEventArgs(RoutedEvent routedEvent,string selectedItem)
            : base(routedEvent)
        {
            _selectedItem = selectedItem; 
        }

       public string SelectedItem
        {
            get { return _selectedItem; }
        }
    }

You need to create custom Routed event in case you want to hook handler from XAML.

Refer to the article here - How to create custom routed event.


public partial class MyUserControl
{
    public static readonly RoutedEvent TabItemSelectedEvent = 
        EventManager.RegisterRoutedEvent("TabItemSelected",
                     RoutingStrategy.Bubble, typeof(RoutedEventHandler), 
                     typeof(MyUserControl));

    public event RoutedEventHandler TabItemSelected
    {
        add { AddHandler(TabItemSelectedEvent, value); } 
        remove { RemoveHandler(TabItemSelectedEvent, value); }
    }

    void RaiseTabItemSelectedEvent()
    {
        RoutedEventArgs newEventArgs =
                new RoutedEventArgs(MyUserControl.TabItemSelectedEvent);
        RaiseEvent(newEventArgs);
    }

    private void TabControl_SelectionChanged(object sender,
                                             SelectionChangedEventArgs e)
    {
        RaiseTabItemSelectedEvent();
    }    
}

Note : You need to create class deriving from RoutedEventArgs in case you want to pass some custom data like string in your case and pass your custom event args while raising an event.

UPDATE :

Like I said you have to create derived class from RoutedEventArgs in case want to pass string value. This is how it will go:

public class SelectedTabRoutedEventArgs : RoutedEventArgs
{
    private readonly string selectedItem;

    public SelectedTabRoutedEventArgs(RoutedEvent routedEvent,
                                      string selectedItem)
        :base(routedEvent)
    {
        this.selectedItem = selectedItem;
    }

    public string SelectedItem
    {
        get
        {
            return selectedItem;
        }
    }
}

and update other methods like this:

void RaiseTabItemSelectedEvent(string selectedItem)
{
    SelectedTabRoutedEventArgs newEventArgs =
          new SelectedTabRoutedEventArgs(SampleUserControl.TabItemSelectedEvent, 
                                         selectedItem);
    RaiseEvent(newEventArgs);
}

private void TabControl_SelectionChanged(object sender,
                                            SelectionChangedEventArgs e)
{
    RaiseTabItemSelectedEvent(tabControl.SelectedValue.ToString());
}

Also you target event handler in window will look like this:

private void TabSelectionChanged(object sender, RoutedEventArgs e)
{
    SelectedTabRoutedEventArgs args = (SelectedTabRoutedEventArgs)e;
    string selectedItem = args.SelectedItem;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM