[英]When do declared XAML list items have their dependency properties set?
Background 背景
I am creating a custom wpf panel. 我正在创建一个自定义的wpf面板。 To help lay out the child items it uses a secondary list (similar to Grid.RowDefinitions or Grid.ColumnDefinitions) that I call "layouts". 为了帮助布置子项,它使用了我称之为“布局”的辅助列表(类似于Grid.RowDefinitions或Grid.ColumnDefinitions)。 Each layout has a couple dependency properties, and child items use an attached property to determine where they're placed as seen below. 每个布局都有一些依赖项属性,子项使用附加属性来确定它们的放置位置,如下所示。
<Panel>
<Panel.Layouts>
<Layout/>
<Layout Attachment="Right"/>
<Layout Attachment="Left" Target="0"/>
</Panel.Layouts>
<ChildItem Panel.Layout="0"/>
<ChildItem Panel.Layout="1"/>
<ChildItem Panel.Layout="2"/>
<Panel/>
Obviously, things are simplified a bit, but long story short: I need to process the layouts as they are added before the "Arrange" process can occur. 显然,事情有点简化,但长话短说:我需要在“排列”过程发生之前处理布局。 I have created a custom collection and I can see the items as they are added (see code below), but the items just have their default properties. 我创建了一个自定义集合,我可以看到添加的项目(参见下面的代码),但这些项目只有默认属性。
LayoutCollection: IList
{
public int IList.Add(object value)
{
// When first starting, this line always returns the default value, not the one set in XAML
Attachment a = (value as Layout).Attachment;
// Other code happens below...
}
}
However, when I look at the collection after the panel has been initialized the properties are all set correctly. 但是,当我在初始化面板后查看集合时,属性都已正确设置。 Which brings me to my question: 这让我想到了我的问题:
Question 题
At what point during the process between XAML and initialization of the panel do the items get assigned their properties, and how are they assigned? 在XAML和面板初始化过程中的哪个阶段,项目会被赋予其属性,以及它们是如何分配的? I need to somehow hook on to that and run a bit of code. 我需要以某种方式挂钩并运行一些代码。
My idea is that the Panel layout system of WPF is not really designed to be intercepted the way you wanted it to be. 我的想法是WPF的Panel布局系统并没有真正按照你想要的方式截取。
But there is a mechanism in-place to 'sync' your custom DependencyProperties. 但是有一种“同步”自定义DependencyProperties的机制。 And the question is WHEN do you need these properties during the layout passes? 问题是在布局过程中你需要这些属性吗?
If it is during ArrangeOverride, you have to set AffectsArrange
. 如果是在ArrangeOverride期间,则必须设置AffectsArrange
。 Or AffectsMeasure
when during MeasureOverride. 或AffectsMeasure
的MeasureOverride期间时。 There are some other flavors of this but I think either of these two can satisfy your requirement. 还有一些其他的味道,但我认为这两个中的任何一个都可以满足您的要求。
For example, if you choose AffectsArrange and your Panel is notified of List.Add, ArrangeOverride will be invoked and the newly added item can be used within your arrangement logic. 例如,如果选择AffectsArrange并且Panel会收到List.Add的通知,则会调用ArrangeOverride,并且可以在排列逻辑中使用新添加的项目。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.