Binding to an element of an array (or List)

After much googling, I haven't found an answer to this.

I have an array of colors. I want to bind a Button's BorderColor to a specific element within an array.
What is the syntax to achieve this?

Xaml:

 BorderColor="{Binding ButtonColors[11], Converter={Services:ByteToBorderColourConverter}}"

ViewModel:

private List<byte> buttoncolors;

public List<Byte> ButtonColors
    {
        get => buttoncolors;
        set
        {
            buttoncolors = value;
            OnPropertyChanged();
        }
    }

...

ButtonColors[11] = (Byte)Data[3];

I've also tried implementing this with List, but it also doesn't work.
I think the xaml binding syntax is incorrect maybe?

Best Answer

Answers

  • DR_BartDR_Bart Member ✭✭
    edited December 2018

    Thank you Harshita. This will work.
    As a further question - do you know how to reference values from an enum in xaml?

    for example, if I have the enum

    enum Index : int
    {
        value1,
        value2,
        value3
    }
    

    Can I then use something like:

    ButtonColor=ButtonColors.ElementAt(Index.value2);
    

    I haven't had a chance to try this, but I will try it and report back.

Sign In or Register to comment.