3

I want to bind an item source in my WPF to a static array. I have tried to do this by writing:

ItemsSource="{Binding XLTT.Core.Models.names}"

But it doesn't work. Why? Additionally, I have seen others bind to objects. Is it ok to bind to a static array or should I be binding to an object instead?

EDIT:

Here is the class with names definition.

namespace XLTT.Core.Models
{
    internal class TTColumn
    {
        internal string ColumnName;
        internal string ColumnType;
        internal int ColumnOrder;
        internal bool IsRequired;
        internal int ColumnWidth;

        public static string[] names = {"Matt", "Joanne", "Robert"};
    }
}
user1283776
  • 19,640
  • 49
  • 136
  • 276
  • Where is defined your static array? Can you show us the class with its definition? – Kryptos Jul 10 '15 at 07:15
  • As long as array is not private and it's property it's OK. http://stackoverflow.com/questions/936304/binding-to-static-property – Maximus Jul 10 '15 at 07:15
  • @Maximus: tried writing ItemsSource="{Binding x:Static local:XLTT.Core.Models.names} but that didn't work – user1283776 Jul 10 '15 at 08:57

2 Answers2

2

For starters, Names needs to be property

public static string[] Names {get; set;}

Then bind as follows:

 ItemsSource="{Binding Source={StaticResource TTColumn}, Path=Names}"
Maximus
  • 3,458
  • 3
  • 16
  • 27
0
<Window
...
xmlns:m="clr-namespace:XLTT.Core.Models">

ItemsSource="{x:Static m:TTColumn.names}"
  • 1
    Code only answers are discouraged. Please add some explanation as to how this solves the problem, or how this differs from the existing answers. [From Review](https://stackoverflow.com/review/low-quality-posts/23987439) – Nick Sep 07 '19 at 06:02