简体   繁体   English

声明的XAML列表项何时设置了依赖项属性?

[英]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.

相关问题 当基类是通用的时,在XAML中设置依赖项属性 - Set dependency properties in XAML when the base class is generic 如何在XAML中设置用户控件的列表项 - How to Set List Items of a usercontrol in XAML 将字符串项绑定到 xaml 中字符串的依赖属性列表 - Binding string items to a dependency property list of strings in xaml 链接依赖项属性xaml wpf - chaining dependency properties xaml wpf 为什么动态添加组合框项目会占据整个屏幕,而在XAML中进行设置时,组合框项目却只能覆盖cbx的宽度? - Why do combobox items take the whole screen if added dynamically but only the cbx's width when set in the XAML? 类型ObservableCollection的DependencyProperty <T> XAML中设置的set包含的项多于声明的项。 C# - A DependencyProperty of Type ObservableCollection<T> set in XAML contains more items than there are declared. c# 未命名的 XAML 项目在哪里申报? - Where are the non-named XAML items declared? 绑定列表数据更改时如何通知XAML属性? - How to notify XAML properties when list data of binding changed? 获取列表中具有另一个列表中的一个属性的所有项目的计数 - Get count of all items in a list that have one of the properties in another list 修改 WPF XAML 中的非依赖属性 - Modifying non-dependency properties in WPF XAML
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM