简体   繁体   中英

c# xaml Managing many bindings(property of an array?)

I am trying to set up many bindings in a xaml page in a UWP c# project so I thought about binding to an array, however I can't figure out how to make this because the elements in a {x:Bind example} styled binding should be properties.

I know(is it correct?) that in c# it isn't possible to have a property linked to an array, I mean something like this(even if it isn't correct):

    Brush[] _myArray = new Brush[50];

    public Brush[n] myArray
    {
        get { return _myArray[n]; }

        set { _myArray[n] = value; }
    }

So I thought(it is a terrible solution) about declaring all of my variables and properties and then adding all the properties to a list and accessing them in c# as items of the list, something like this to be clear:

    Brush _myBrush1;
    public Brush myBrush1{...}
    List<Brush> myBrushList = new List<Brush>();

    private void initialize()
    {
        myBrushList.Add(myBrush1);
    }

But then with

    myBrushlist[0] = new SolidColorBrush(Colors.red);

nothing happens, it's like I set the element of the list(and not the property) to be a new brush.

What's the right way to do this? If there isn't a "right way" How could I correct my code?

As suggested the right way to do what I was looking for was using a resource dictionary, below some code:

xaml

...
<Page.Resources>
    <SolidColorBrush x:Key="n1" Color="Red"></SolidColorBrush>
    <SolidColorBrush x:Key="n2" Color="Red"></SolidColorBrush>
</Page.Resources>
...
<Grid Background="{StaticResource n1}"/>
<Grid Background="{StaticResource n2}"/>

c#

SolidColorBrush myResource= (SolidColorBrush)this.Resources["n" + Convert.ToString(myIndexInt)];
myResource.Color = Colors.Red;

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