在 XAML 中使用 [“字符串”] 语法进行数据绑定

本文关键字:字符串 语法 数据绑定 XAML | 更新日期: 2023-09-27 18:30:34

如果我有

public class Foo : INotifyPropertyChanged{
    public object this[string name] {
        get {
            PremiseProperty prop;
            if (_properties.TryGetValue(name, out prop))
                return prop.Value;
            return null;
        }
        set {
            SetMember(name, value);
        }
    }
   ...
}

在我可以做的代码中

var f = new Foo();
f["Charlie"] = 42;
Debug.WriteLine(f["Charlie"]);

如何访问 XAML 中的"属性"查理"?

不是这些:

<TextBlock Text="{Binding Path=[Charlie]}"/> // hangs designer
<TextBlock Text="{Binding Path=["Charlie"]}"/> // bad syntax
<TextBlock Text="{Binding Path=['Charlie']}"/> // bad syntax

在 XAML 中使用 [“字符串”] 语法进行数据绑定

设置 DataContext 后,试试这个(更新):

<TextBlock Text="{Binding [Charlie]}"/>