Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 404
Bradley Uffner Avatar asked Oct 18 '17 18:10

Bradley Uffner


People also ask

What is markup extension in XAML?

Markup extensions are a XAML technique for obtaining a value that's not a primitive or a specific XAML type. For attribute usage, markup extensions use the known character sequence of an opening curly brace { to enter the markup extension scope, and a closing curly brace } to exit.

Is XAML a markup?

In WPF and UWP, XAML is a user interface markup language to define UI elements, data binding, and events.

What is the advantage of using XAML markup extensions?

XAML markup extensions help extend the power and flexibility of XAML by allowing element attributes to be set from sources other than literal text strings. In either case, the text string set to the Color attribute is converted to a Color value by the ColorTypeConverter class.

What are markup extensions in WPF?

The most common markup extensions used in WPF programming are those that support resource references ( StaticResource and DynamicResource ), and those that support data binding ( Binding ). StaticResource provides a value for a property by substituting the value of an already defined resource.


1 Answers

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;
    }
}
like image 187
Mahdi Rastegari Avatar answered Oct 26 '22 16:10

Mahdi Rastegari