如果我将 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 中同样可以正常工作。