AvailableValuesAnnotation

Hi,

I want to create a custom annotation to get a drop down of values and select one. Simular as selecting a Instrument in a TestStep. My example code shows a drop down in the Editor, but selecting doesn’t work.
Any ideas how to solve/implement this?

namespace AvailableValuesAnnotationDemo
{
    public class MyInt
    {
        public int Value { get; set; }

        public override string ToString()
        {
            return Value.ToString();
        }
    }

    public class MyIntAnnotation : IAvailableValuesAnnotation, IStringReadOnlyValueAnnotation
    {
        AnnotationCollection annotations;

        public MyIntAnnotation(AnnotationCollection annotations)
        {
            this.annotations = annotations;
        }

        public IEnumerable AvailableValues 
        { 
            get
            {
                for (int i = 0; i < 10; i++)
                    yield return new MyInt() { Value = i + 1 };
            }
        }

        public string Value 
        {
            get => (annotations.Get<IObjectValueAnnotation>()?.Value as MyInt)?.ToString();
            set => throw new NotSupportedException("not supported");
        }
    }

    public class MyIntAnnotator : IAnnotator
    {
        public double Priority => 5;

        public void Annotate(AnnotationCollection annotations)
        {
            var member = annotations.Get<IMemberAnnotation>()?.Member;

            if (member == null)
                return;

            if (member.TypeDescriptor == TypeData.FromType(typeof(MyInt)) == false) return;

            annotations.Add(new MyIntAnnotation(annotations));
        }
    }
}

afbeelding

@steve.vandenbussche welcome to the community! I noticed you also posted this on Gitlab, but I just wanted to update here so everyone could follow, and just in case you didn’t see it:

@rolf_madsen’s answer from Gitlab:
"The reason why your class cannot be selected is because MyInt(1) is not equal to another instance of MyInt(1), so when the UI compares the currently selected MyInt with the ones from the list there is no match, which means the selected value is not present in the list, which gives the blank UI behavior.

To fix the problem, override GetHashCode and Equals to make MyInt with the same integer values comparable."

Thanks, this solved my problem!

2 Likes

@steve.vandenbussche Welcome! This is really interesting. I have not personally used custom annotations, and am wondering what I might be missing out on. If I may ask, what functionality are you achieving here that couldn’t be achieved with the typical AvailableValues attribute approach?

1 Like