绑定到多个索引器 [英] Binding to multiple indexers

查看:60
本文介绍了绑定到多个索引器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将一个索引属性与两个索引器绑定.该属性看起来像这样

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; }
}

根据 http://msdn.microsoft.com/zh-CN/library/ms742451.aspx ,可以绑定像这样的索引属性

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...]" .../>

甚至有一个例子:

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

但是,当我尝试像这样在XAML中访问该属性时:

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:

未命名的参数"0" .Image"必须出现在命名参数之前.

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

似乎将0] .Image解释为下一个参数.我想念什么?

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

推荐答案

问题是{Binding}标记扩展名-带有delimiter,.

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; }
}

这篇关于绑定到多个索引器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆