我有一个继承自 DynamicObject 的类,除了动态属性之外,它还有一些静态定义的属性。DataTemplate
静态定义的属性可以毫无问题地绑定到 a 中- 但不是动态属性。
我正在为 WP8 使用 Silverlight - 不确定这是否与 WPF 相同。
DynamicObjects 是否支持绑定?
编辑:这是代码的摘录:
DynamicObject
班级ItemContent
:_
public class ItemContent : DynamicObject, INotifyPropertyChanged
{
private Dictionary<string, object> propertyBag = new Dictionary<string, object>();
// Statically-defined Property
public string SProperty { get; set; }
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
return propertyBag.TryGetValue(binder.Name, out result);
}
public override bool TrySetMember(SetMemberBinder binder, object value)
{
if (propertyBag.ContainsKey(binder.Name)){
propertyBag[binder.Name] = value;
} else {
propertyBag.Add(binder.Name, value);
}
RaisePropertyChanged(binder.Name);
return true;
}
public override IEnumerable<string> GetDynamicMemberNames()
{
return propertyBag.Keys;
}
// ... omitted for brevity
}
XAML:
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding SProperty}"/>
<TextBlock Text="{Binding DProperty}"/>
</StackPanel>
</DataTemplate>
摘自 ViewModel:
dynamic i1 = new ItemContent() { SProperty = "static property" };
i1.DProperty = "dynamic property";
ObservableCollection<ItemContent> Items = new ObservableCollection<ItemContent>(){ i1 };