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:22 PM

Answers

  • I think you have to provide Custom Intellisense in VisualStudio for your custom event.

    There is a detailed description of the process in this article.

    Providing Custom Intellisense in VS.NET IDE

    Or you could install a Resharper softerwall.

    Hope it can help you.

    ----------------------------------------------------------

    Please mark this thread as answer if you think it is helpful.

    Thank you!

    • Proposed as answer by Tao Liang Thursday, January 15, 2009 2:32 AM
    • Marked as answer by Tao Liang Friday, January 16, 2009 1:51 AM
    Thursday, January 15, 2009 2:21 AM