如果我讓視圖模型定義如下:的Silverlight綁定到一個項目在一個字典
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。
你有沒有嘗試讓你的ViewModel [自定義類型描述符](http://msdn.microsoft.com/en-us/library/system.componentmodel.icustomtypedescriptor.aspx)?我不知道Silverlight是否支持這一點。 – Will 2011-05-10 13:14:53