简单附加的依赖项属性集合? [英] Simple Attached Dependency Properties Collection?

查看:88
本文介绍了简单附加的依赖项属性集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有点挣扎.如果我的课程如下:

Hi All,

I''m struggling a bit. If I have classes as below:

public static class ClassName
{
    ...attached DP called 'ChildObjects', of type ChildCollection....
}





public class ChildCollection : ObservableCollection<child>
{
}
</child>



我可以将附加的依赖项属性定义添加到ClassCollection类型为ChildCollection的ClassName中,从而可以执行以下操作:



I can add an attached dependency property definition to ClassName, of type ChildCollection, allowing me to do the below:

<grid>
  <classname.childobjects>
    <childcollection>
      <child someproperty="{Binding Etc}" />
      <child someproperty="{Binding Etc}" />
    </childcollection>
  <classname.childobjects>
<grid></grid></classname.childobjects></classname.childobjects></grid>



这很好用-子对象是从FrameworkElement派生的,因此具有数据上下文.唯一的问题是ChildCollection-它不是框架元素,因此它意味着它不在逻辑树中,因此对Child对象的绑定不起作用-更不用说使用"FindAncestor"之类的东西了. br/>
有没有标准的方法来解决这个问题?我一直在寻找可以将子级添加到逻辑树但没有运气的"FrameworkElementCollection".在深入研究之前,我想知道我是否缺少明显的东西!

预先感谢,

Dave



This works great - Child objects are derived from FrameworkElement and therefore have a datacontext. The only problem is ChildCollection - it is not a framework element therefore it means that it''s not in the logical tree, so bindings on the Child objects don''t work - let alone using things like ''FindAncestor''.

Is there a standard way to deal with this? I''ve looked for ''FrameworkElementCollection'' that would add the children to the logical tree but with no luck. Before I dive into creating one, I would like to know if I''m missing something obvious!

Thanks in advance,

Dave

推荐答案

您非常接近看看 ^ ]
You''re very close have a look at the UIElementCollection[^]


确定通过更多的研究,我确切地知道了如何执行此操作-使该集合成为FreezableCollection< t>并确保您要从中进行收集的类是从Freezable派生的-然后您将datacontext设置为应有的设置:)

Nishant在这里有一个很好的例子:

一种MVVM友好方法,用于在WPF应用程序中添加系统菜单项 [
OK With a bit more research I found out exactly how to do this - make the collection a FreezableCollection<t> and make sure that the class you are making a collection from is derived from Freezable - then you get the datacontext set just as you should :)

There''s a great example by Nishant here:

An MVVM friendly approach to adding system menu entries in a WPF application[^]


这篇关于简单附加的依赖项属性集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆