gpt4 book ai didi

c# - 集合类型附加属性

转载 作者:行者123 更新时间:2023-11-30 23:07:16 25 4
gpt4 key购买 nike

我想延长 FrameworkElement Button 等元素按收集的收集类型附加属性分类DependencyObjects .

困难在于集合项的绑定(bind)不起作用:没有出现调试或运行时错误,但从未调用绑定(bind)的源。

我注意到集合类型的附加属性不继承自类 DependencyObject .

我猜 DataContext属性(property)将由任何 child 继承DependencyObject对象(只要父对象也是 DependencyObject 对象)。由于集合类型的附加属性不继承自 DependencyObject DataContext不会发生属性(property)继承。

  1. 我想知道为什么 DependencyObject 的实例可以继承DataContext属性为 DataContextFrameworkElement 中定义的属性? DependencyObject如何管理 DataContext查找?
  2. 为什么用ElementName=PageName指定绑定(bind)源效果不佳(例如 {Binding MyProperty="{Binding DataContext.PropertySource1, ElementName=PageName} )?如果DependencyObject还负责ElementName查找,它是如何做到的?
  3. 有没有继承DependencyObject的UWP集合? (在 WPF 中有 FreezableCollection<T> 类,但我在 UWP 环境中找不到挂件。)

下面的 XAML 标记显示了一个示例扩展,其中 Binding不起作用。

<Button Name="Button">
<ext:MyExtension.MyCollection>
<ext:MyDependencyObject MyProperty="{Binding PropertySource1}"/>
<ext:MyDependencyObject MyProperty="{Binding PropertySource1}"/>
</ext:MyExtension.MyCollection>
</Button>

如果我对非集合类型的附加属性执行以下扩展,则可以正确解析绑定(bind)。

<Button Name="Button">
<ext:MyExtension.MyProperty>
<ext:MyDependencyObject MyProperty="{Binding PropertySource1}"/>
</ext:MyExtension.MyProperty>
</Button>

下面的代码显示了一个示例集合类型的附加属性实现。考虑附加属性类还包含一个非集合类型附加属性的定义(它与绑定(bind)一起正常工作)。

public class MyDependencyObject: DependencyObject
{
public object MyProperty
{
get { return (object)GetValue(MyPropertyProperty ); }
set { SetValue(MyPropertyProperty , value); }
}
public static readonly DependencyProperty MyPropertyProperty =
DependencyProperty.Register("MyProperty", typeof(object), typeof(MyProperty), null);
}

public class MyPropertyCollection : ObservableCollection<MyDependencyObject> { }

public static class MyExtension
{
// Collection-type AttachedProperty with DependencyObject items

public static MyPropertyCollection GetMyPropertyCollection(DependencyObject obj)
{
MyPropertyCollection collection = (MyPropertyCollection )obj.GetValue(MyCollectionProperty );
if (collection == null)
{
collection = new MyPropertyCollection();

collection.CollectionChanged +=
(sender, e) =>
{
//intiailization of elements possible
};


obj.SetValue(MappingsProperty, collection);
}

return collection;
}

public static void SetMyPropertyCollection(DependencyObject obj, MyPropertyCollection value)
{
obj.SetValue(MyCollectionProperty , value);
}

public static readonly DependencyProperty MyCollectionProperty =
DependencyProperty.RegisterAttached("MyCollection", typeof(MyPropertyCollection), typeof(MyExtension), null);


// DependencyObject-type AttachedProperty

public static MyProperty GetMapping(DependencyObject obj)
{
return (MyProperty )obj.GetValue(MyPropertyProperty );
}

public static void SetMapping(DependencyObject obj, MyProperty value)
{
obj.SetValue(MyPropertyProperty , value);
}

public static readonly DependencyProperty MyPropertyProperty =
DependencyProperty.RegisterAttached("MyProperty", typeof(MyDependencyObject), typeof(MyExtension), null);

}

最佳答案

在 WPF 中,可以通过使用 FreezableCollection<T>或者通过继承 Freezable类,IList<T>IList :

  1. 继承自 DependencyObject因为无法访问 DependencyObject 的两个必需方法,但是 Freezable它们周围有 wrapper 。
  2. 实现 IList<T>IList随心所欲。
  3. 向集合中添加新元素时调用 OnFreezablePropertyChanged(null, newItem) .
  4. 从中删除元素时调用 OnFreezablePropertyChanged(item, null) .

OnFreezablePropertyChanged方法内部会调用 DependencyObject 的两个方法提供或删除继承上下文 ( source )。

但是在 UWP 中没有 Freezable并且没有这样的方法,所以在 Windows 10.0.10240.0 之前是不可能的。但是,如果您的目标是 v10.0.10240.0 或更高版本,则应使用 DependencyObjectCollection 这是为行为而生的。

The purpose of the DependencyObjectCollection class is mainly to support the tooling and portability of behaviors. Behaviors are a technique for defining certain basic interactions of a UI element entirely in XAML, without requiring an event handler and code-behind.

因此,在 UWP 和 WPF 中,仅当子项是逻辑/可视子项或者它们是依赖属性值时,才能将继承上下文提供给子项。正因为如此 Binding带套装 ElementName在您的情况下使用时不起作用。 ElementName当绑定(bind)附加到依赖对象时,属性用于在运行时解析对象,而不是在编译时。

UWP 绑定(bind)的工作方式类似于 WPF 绑定(bind),因此例如请参阅 ElementObjectRef.GetObject 来自第二个平台。

关于c# - 集合类型附加属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47471077/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com