PropertyChanged用于索引器属性

Inf*_*ris 33 c# data-binding wpf indexer

我有一个带有索引器属性的类,带有一个字符串键:

public class IndexerProvider {
    public object this[string key] {
        get
        {
            return ...
        }
        set
        {
            ...
        }
    }

    ...
}
Run Code Online (Sandbox Code Playgroud)

我使用索引符号绑定到WPF中此类的实例:

<TextBox Text="{Binding [IndexerKeyThingy]}">
Run Code Online (Sandbox Code Playgroud)

这工作正常,但我想PropertyChanged在其中一个索引器值更改时引发事件.我尝试使用属性名称"[keyname]"(即在键的名称周围包括[])来提升它,但这似乎不起作用.我的输出窗口中没有任何绑定错误.

我不能使用CollectionChangedEvent,因为索引不是基于整数的.从技术上讲,该对象无论如何都不是一个集合.

我可以这样做,等等,怎么样?

Jb *_*ain 46

根据这篇博客文章,你必须使用"Item[]".Item是编译器在使用索引器时生成的属性的名称.

如果要显式,可以使用IndexerName属性修饰indexer 属性.

这将使代码看起来像:

public class IndexerProvider : INotifyPropertyChanged {

    [IndexerName ("Item")]
    public object this [string key] {
        get {
            return ...;
        }
        set {
            ... = value;
            FirePropertyChanged ("Item[]");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

至少它使意图更加清晰.我不建议您更改索引器名称,如果您的伙伴发现字符串是"Item[]"硬编码的,则可能意味着WPF无法处理不同的索引器名称.

  • 这个解决方案很好用,但它有一个恼人的限制:你不能指定只为一个键更改值......所以如果你有很多键的绑定,它们都会被刷新 (2认同)

gho*_*ord 15

另外,你可以使用

FirePropertyChanged ("Item[IndexerKeyThingy]");
Run Code Online (Sandbox Code Playgroud)

仅通知索引器上绑定到IndexerKeyThingy的控件.

  • 有没有人成功地让这个工作?它不适合我.只有"Item []"似乎会触发更新. (6认同)
  • 它在Silverlight 4/5上工作正常,但在WPF 4.6.2上它根本不起作用.还是要使用Item []. (3认同)

小智 6

在处理INotifyPropertyChang(ed/ing)和索引器时,至少还有一些注意事项.

首先,大多数避免魔术属性名称字符串的流行方法都是无效的.该[CallerMemberName]属性创建的字符串最后缺少'[]',并且lambda成员表达式在表达概念方面存在问题.

() => this[]  //Is invalid
() => this[i] //Is a method call expression on get_Item(TIndex i)
() => this    //Is a constant expression on the base object
Run Code Online (Sandbox Code Playgroud)

一些其他 的职位已经使用Binding.IndexerName,以避免字符串文字"Item[]",这是合理的,但引出了第二个潜在的问题.对WPF相关部分的分解的研究发现了PropertyPath.ResolvePathParts中的以下部分.

if (this._arySVI[i].type == SourceValueType.Indexer)
  {
    IndexerParameterInfo[] array = this.ResolveIndexerParams(this._arySVI[i].paramList, obj, throwOnError);
    this._earlyBoundPathParts[i] = array;
    this._arySVI[i].propertyName = "Item[]";
  }
Run Code Online (Sandbox Code Playgroud)

重复使用"Item[]"作为常量值表明WPF期望它是在PropertyChanged事件中传递的名称,并且,即使它不关心实际属性的调用(我没有确定我满意的一个)方式或其他),避免使用[IndexerName]将保持一致性.


小智 5

实际上,我认为将IndexerName属性设置为"Item"是多余的.如果要为其集合项指定其他名称,则IndexerName属性专门用于重命名索引.所以你的代码看起来像这样:

public class IndexerProvider : INotifyPropertyChanged {

    [IndexerName("myIndexItem")]
    public object this [string key] {
        get {
            return ...;
        }
        set {
            ... = value;
            FirePropertyChanged ("myIndexItem[]");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

将索引器名称设置为您想要的任何名称后,您就可以在FirePropertyChanged事件中使用它.