Bradley Uffner
Bradley Uffner

Reputation: 17001

Provide Intellisense for Custom MarkupExtension in XAML

I've created a custom MarkupExtension that allows an easy way to to forward events from FrameworkElements to methods on the view-model. Everything works perfectly, except Visual Studio doesn't provide any Intellisense when filling in the XAML. Is there any way that I can provide Visual Studio with the information it needs to provide Intellisense?

The MarkupExtension:

public class VmCall : MarkupExtension
{
    public string ActionName { get; }

    public VmCall(string actionName)
    {
        ActionName = actionName;
    }
    //.....
}

The view-model:

class TestWindowVm : ViewModel
{
    public void Click()
    {
        MessageBox.Show("Click!");
    }
}

Using the MarkupExtension in XAML:

<Window
    x:Class="WpfLib.Tests.VmCallMarkupExtension.TestWindow"   
    xmlns:wpfLib="clr-namespace:AgentOctal.WpfLib;assembly=WpfLib"
    //Many xmlns and attributes left out of example for brevity.
    d:DataContext="{d:DesignInstance TestWindowVm}"
    mc:Ignorable="d">
    <Button Click="{wpfLib:VmCall Click}"/>
</Window>

Ideally, when the user presses the space-bar after "VmCall", Intellisense should present them with a list of all the methods on the current element's DataContext (Just Click on TestWindowVm in this specific example).

If there is some way to make this work, based on previous experience with WinForms, I would expect some kind of Attribute I could place on the arguments of the constructor for VmCall that informs Visual Studio how to generate the intellisense for that argument. So far, I haven't been able to find anything like that. I'm not sure if I'm actually looking for the wrong thing, or if this in't possible to do at all. If it matters, I'm mainly interested in Visual Studio 2017.

I've looked at all the attributes listed in System.Windows.Markup, and nothing obvious jumped out at me.

Upvotes: 17

Views: 1297

Answers (1)

Mahdi Rastegari
Mahdi Rastegari

Reputation: 103

you have created a MarkupExtension with the string input. how can Intellisense know about which string you want? but if you, for example, use an enum, the Intellisense suggested you many things.

public enum MyEnum
    {
        first , second 
    }

public class VmCall : MarkupExtension
{
    public MyEnum ActionName { get; }

    public VmCall(MyEnum actionName)
    {
        ActionName = actionName;
    }
    //.....
    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return this;
    }
}

Upvotes: 1

Related Questions