locked
Intellisense not working in XAML if used on a Custom event handler RRS feed

  • Question

  • I created a UserControl and a fully custom (args, handler, event) event for it:

    public partial class UserControl1 : UserControl { 
        public UserControl1() { 
            InitializeComponent(); 
        } 
        public static RoutedEvent AreaSelectedEvent = 
                EventManager.RegisterRoutedEvent("AreaSelected", RoutingStrategy.Bubble, typeof(AreaSelectEventHandler), typeof(UserControl1)); 
        public event AreaSelectEventHandler AreaSelected { add { AddHandler(AreaSelectedEvent, value); } remove { RemoveHandler(AreaSelectedEvent, value); } } 
        protected virtual void OnAreaSelected() { 
            RoutedEventArgs args = new AreaSelectEventArgs() { RoutedEvent = AreaSelectedEvent, Source = this };
            RaiseEvent(args); 
        } 
     
    public delegate void AreaSelectEventHandler(object sender, AreaSelectEventArgs e); 
     
    public class AreaSelectEventArgs : RoutedEventArgs {}

    It is working as intended but, if I use the UserControl in XAML, the XAML editor does not help me. ie: it is not creating the event handler for the custom event. It shows (lists) the event in the popul list, and creates what you see below:

    <Window x:Class="Test.Window1" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:local="clr-namespace:Test" 
        Title="Window1" Height="300" Width="300"
        <Grid> 
            <local:UserControl1 AreaSelected="|" /> 
        </Grid> 
    </Window> 

    If I place the cursor where the pipe (|) is and press Control+Space it does not show anything and complains about the empty string. If I fill it in like: <local:UserControl1 AreaSelected="uc_AreaSelected" /> and right click it and press Navigate to evnt handler... it tells me:
    Unable to navigate to event handler. Valid event assignment not found. Select an event handler in the XAML and try again.
    even if I create an event handler like so: public void uc_AreaSelected(object sender, AreaSelectEventArgs e) {}

    Why is it not working what am I doing wrong when creating custom event and/or using the User control?

    Comment:
    At some place (ScrollEventArgs) I saw that it's implemented like this (which does not help either):
    protected override void InvokeEventHandler(Delegate genericHandler, object genericTarget) { 
        AreaSelectEventHandler handler = (AreaSelectEventHandler)genericHandler; 
        handler(genericTarget, this); 
    I don't know why it is needed, but I think it is to make sure (by casting which throws Exception) that the right type of handler is used.
    Sunday, January 11, 2009 6:25 PM

Answers

  • Aw..I got it: Instead of using 

    public   event  AreaSelectEventHandler AreaSelected

    you have to write:

    public   event EventHandler<AreaSelectedEventArgs> AreaSelected

    and also when registering this event:

    public   static  RoutedEvent AreaSelectedEvent = 
                EventManager.RegisterRoutedEvent( "AreaSelected" , RoutingStrategy.Bubble,  typeof (EventHandler< AreaSelectEventArgs> ),  typeof (UserControl1)); 

    Now, your Intellisense inside XAML will be working fine!
    Monday, February 15, 2010 1:50 PM

All replies

  • Hi, did you find a solution why the intellisense isn't working? At the moment I'm facing the same problem...maybe there is a missing class attribute.

    Greets, Michael
    Monday, February 15, 2010 12:20 PM
  • Aw..I got it: Instead of using 

    public   event  AreaSelectEventHandler AreaSelected

    you have to write:

    public   event EventHandler<AreaSelectedEventArgs> AreaSelected

    and also when registering this event:

    public   static  RoutedEvent AreaSelectedEvent = 
                EventManager.RegisterRoutedEvent( "AreaSelected" , RoutingStrategy.Bubble,  typeof (EventHandler< AreaSelectEventArgs> ),  typeof (UserControl1)); 

    Now, your Intellisense inside XAML will be working fine!
    Monday, February 15, 2010 1:50 PM