简体   繁体   中英

Can I setup IntelliSense suggestions which depend on another property?

Okay this is probably a naive question but I want to get IntelliSense suggestions on a property for multiple enums. Well. Kinda. But let me explain.

I got a couple of enums like so

public static class Icons 
{
    public enum Regular { /* ... */ }
    public enum Solid{ /* ... */ }
    public enum Brands { /* ... */ }
}

public enum Styles { /* ... */ }

Now I got a XAML Control with two DependencyProperties

public static readonly DependencyProperty MyStyleProperty = DependencyProperty.Register(nameof(MyStyle), typeof(Styles), typeof(Icon), new FrameworkPropertyMetadata(Styles.None, FrameworkPropertyMetadataOptions.AffectsRender, OnUpdateControl));

public static readonly DependencyProperty MyIconProperty = DependencyProperty.Register(nameof(MyIcon), typeof(object), typeof(Icon), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender, OnUpdateControl));

Control.Style accepts values from the enum Styles .
Control.Icon accepts values Icons.[Regular, Solid, Brands] .

The IntelliSense suggestions for Icon depends on the selected Style . So if Control.Style is Styles.Regular then show only suggestions for Icons.Regular on Control.Icon .

It is theoretically possible to add a CompletionProvider that would permit Intellisense to handle your single class, but that would be overkill for such a common issue. Instead, use an enum class or use category classes.

Enum class (not quite, since the constructor is public, but in this case having it open seems logical):

public sealed class IconImage
{
    public Uri ImageUri { get; }
    public IconImage(Uri imageUri)
    {
        this.ImageUri = imageUri;
    }

    public static class ApplicationActions
    {
        public readonly IconImage Save = new IconImage(new Uri("foo"));
        public readonly IconImage Open = new IconImage(new Uri("foo"));
        public readonly IconImage Print = new IconImage(new Uri("foo"));
    }

    public static class Marks
    {
        public readonly IconImage Check = new IconImage(new Uri("foo"));
        public readonly IconImage Tickbox = new IconImage(new Uri("foo"));
        public readonly IconImage CheckedTickbox = new IconImage(new Uri("foo"));
    }

    // ...
}

// Usage: control.Icon = IconImage.Marks.TickBox;
// XAML (without TypeConverter): Icon="{x:Static local:IconImage.Marks.TickBox}"
// XAML (with TypeConverter): Icon="Marks.TickBox"

Category Classes:

public enum IconImage
{
    Save, Open, Print,
    Check, Tickbox, CheckedTickbox,
    // ...
}

public static class IconImages
{
    public static class Application
    {
        public static readonly IconImage
            Save = IconImage.Save,
            Open = IconImage.Open,
            Print = IconImage.Print;
    }

    public static class Marks
    {
        public static readonly IconImage
            Check = IconImage.Check,
            Tickbox = IconImage.Tickbox,
            CheckedTickbox = IconImage.CheckedTickbox;
    }

    // … other category classes …
}

// Usage: control.Icon = IconImage.Print  -- or --  control.Icon = IconImages.Application.Print
// XAML: Icon="Print" -- or -- Icon="{x:Static local:IconImages.Application.Print}"

With either route, I would suggest using a tool like T4 or XSLT to dynamically generate the code. It is very easy to introduce bugs when trying to maintain large lists manually.

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