【问题标题】:DynamicObject's dynamic property does not bindDynamicObject 的动态属性不绑定
【发布时间】:2013-08-17 16:35:29
【问题描述】:

我有一个继承自 DynamicObject 的类,除了动态属性之外,它还有一些静态定义的属性。静态定义的属性绑定在 DataTemplate 中没有问题 - 但不是动态属性。

我正在为 WP8 使用 Silverlight - 不确定这是否与 WPF 相同。

DynamicObjects 是否支持绑定?

编辑:以下是代码摘录:

DynamicObjectItemContent

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


【解决方案1】:

Silverlight 中似乎存在错误。

Here's the report.

该页面的“解决方法”标签包含 4 种不同的解决方法,每种方法都有其优缺点。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-14
    • 2020-03-22
    相关资源
    最近更新 更多