索引器属性的属性更改

问题描述 投票:0回答:4

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

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

    ...
}

我使用索引器表示法绑定到 WPF 中此类的实例:

<TextBox Text="{Binding [IndexerKeyThingy]}">

工作正常,但我想在索引器值之一发生更改时引发

PropertyChanged
事件。我尝试使用属性名称“[keyname]”(即在键名称周围包含 [])来引发它,但这似乎不起作用。我的输出窗口中没有出现任何绑定错误。

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

我可以这样做吗?怎样做?

c# wpf data-binding indexer
4个回答
55
投票

根据此博客条目,您必须使用

"Item[]"
。 Item 是编译器使用索引器时生成的属性名称。

如果您想明确,可以使用 IndexerName 属性来装饰索引器属性。

这将使代码看起来像:

public class IndexerProvider : INotifyPropertyChanged {

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

至少让意图更加清晰。不过,我不建议您更改索引器名称,如果您的朋友发现字符串

"Item[]"
是硬编码的,则可能意味着 WPF 将无法处理不同的索引器名称。


18
投票

此外,您还可以使用

FirePropertyChanged ("Item[IndexerKeyThingy]");

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


9
投票

在处理 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

几个 other posts 使用

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[]";
  }

重复使用

"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[]");
        }
    }
}

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

© www.soinside.com 2019 - 2024. All rights reserved.