3

I am trying to bind an indexed property with two indexers. The property looks like this

public Item this[int x, int y]
{
  get { return _items[x, y]; }
  set { _items[x, y] = value; }
}

According to http://msdn.microsoft.com/en-us/library/ms742451.aspx, it is possible to bind against indexed properties like that

<object Path="propertyName[index,index2...]" .../>

There is even an example:

<Rectangle Fill="{Binding ColorGrid[20,30].SolidColorBrushResult}" .../>

However when I try to access that property in XAML like that:

<Image Source="{Binding Items[0,0].Image}" />

I get an error in the designer:

The unnamed argument "0].Image" must appear before named arguments.

It seems to interpret 0].Image as the next argument. What am I missing?

Igor Lankin
  • 1,416
  • 2
  • 14
  • 30

2 Answers2

5

The problem is the {Binding} markup extension - which has a delimiter which is ,.

To work around that you can use the following notation...

<TextBox Width="100" Height="100">
    <TextBox.Text>
        <Binding Path="MyIndexer[1,1]" />
    </TextBox.Text>
</TextBox>

Or use the 'escaped' , with \ - which is also in that link (but somehow they're getting over fact that their original notation doesn't work).

<TextBox Text="{Binding MyIndexer[2\,2]}" Width="100" Height="100" />  

Note that indexer, multi-dimentional array syntax is like this :)...

public string this[int x, int y]
{
    get { return _items[x][y]; }
    set { _items[x][y] = value; }
}
NSGaga-mostly-inactive
  • 14,052
  • 3
  • 41
  • 51
  • Thanks! It works within the tag. As for escaping, I tried that already, but then the parameters seem to be interpreted as a single string parameter and the binding doesn't work with the error message "Parameter count mismatch". – Igor Lankin Apr 07 '13 at 20:23
  • you're welcome - yes, they seem to have made their example for this `Path` like I did first - doesn't work w/o escaping it. – NSGaga-mostly-inactive Apr 07 '13 at 20:38
  • brilliant been hunting for this :) – NDJ Oct 15 '13 at 10:35
0

Windows Phone is not a WPF, it is mostly Silverlight, and Silverlight does not support Indexer:

  • Only one-dimensional array indexing is supported.

You can try to fix this by:

a) Try to implement something like Items[0][0], so Items[0] will give you an array to which you again can apply indexer.

b) Try to implement this logic with IValueConverter.

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
outcoldman
  • 11,584
  • 2
  • 26
  • 30