【发布时间】:2013-08-17 16:35:29
【问题描述】:
我有一个继承自 DynamicObject 的类,除了动态属性之外,它还有一些静态定义的属性。静态定义的属性绑定在 DataTemplate 中没有问题 - 但不是动态属性。
我正在为 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 };
【问题讨论】:
-
支持。如果您需要更多帮助,请发布代码。
-
@mdm20,我添加了代码的相关部分。
标签: wpf silverlight windows-phone-7 data-binding windows-phone-8