1

如果我将 ViewModel 定义为以下内容:

public class MainViewModel : DynamicObject
{
    public Dictionary<string, string> Attributes { get; set; }
    public MainViewModel()
    {
        Attributes = new Dictionary<string, string>();
        Attributes["Welcome"] = "Welcome to MVVM Light";
    }

    public override bool TryGetMember(GetMemberBinder binder, out object result)
    {
        if (Attributes.ContainsKey(binder.Name))
        {
            result = Attributes[binder.Name];             
        }
        else
            result = "";
        return true;
    }
}

在silverlight中,我收到以下错误:

System.Windows.Data Error: BindingExpression path error: 'Welcome' property not found on 'DictionaryBasedVM.ViewModel.MainViewModel' 'DictionaryBasedVM.ViewModel.MainViewModel' (HashCode=30542218). BindingExpression: Path='Welcome' DataItem='DictionaryBasedVM.ViewModel.MainViewModel' (HashCode=30542218); target element is 'System.Windows.Controls.TextBlock' (Name=''); target property is 'Text' (type 'System.String')..

在 WPF 中同样可以正常工作。

4

2 回答 2

1

试试这个“{Binding Attributes[Welcome]}”

于 2011-05-10T12:15:59.330 回答
1

问题是DynamicObject只有当引用由类型为 的标识符持有时才会起作用dynamic

然而,Silverlight Xaml 处理与objectnot一起dynamic使用,并使用反射来确定它需要的属性信息。

Oliver 指出的一种选择是使用 Silverlight 与string基于索引器的功能。

于 2011-05-10T12:44:10.753 回答