当孩子的宽度势必父母的ActualWidth的WPF会发生什么 [英] WPF What happens when a child's width is bound to a parent's actualwidth

查看:309
本文介绍了当孩子的宽度势必父母的ActualWidth的WPF会发生什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,如果我有一个绑定放置在它链接到其父母的 ActualWidth的,会发生什么?


$子对象的宽度b $ b

我的猜测是,孩子多大宽度希望家长措施后,孩子告诉家长0宽度,那么母公司给予安排在实际的空间,并试图给孩子为零,因为孩子没不想任何。然后给予到母体的空间的实际宽度应引起的结合来改变孩子的宽度。在这一点上我猜再次进行布局。



不过,这种假设绑定不传播快。我还是当绑定的值传播到目标上的朦胧。这一切都取决于父母的ActualWidth的值更改时。布局是否结束后这种情况发生?然后结合件更新?不中断当前正在运行的代码的每一结合去更新目标价值?如果没有,是不是造成问题,如果一个绑定传播一个需要重绘的改变,然后又结合传播导致重绘不同的变化等。



有人问我的实际问题是什么:



所以,本来我想有一个控制拉伸以填充可用空间。很简单,但是我想有它在一个ScrollViewer中。 ScrollViewer中给出了测量过程中,其子无限空间。因此,你可以绑定子控件父的ActualWidth的和实际高度的宽度和高度;布局做了第二次,和一切似乎肿胀。



不过,后来我有一个的ControlTemplate伸展控制类似的一种问题,但后来发现,我可以设置了minWidth和对齐=舒展有它舒展。



不过,我清楚地记得试图较早我的其他控件,因此它无法工作,所以我回去,并试图找出这两种情况之间的差异。基本上它归结为他们在一个StackPanel中几级了正在之一。



所以,现在我使用一个具有约束力的和其他的了minWidth加对准方法。
反正只是在这个有兴趣,以确保我做事情的方式不会在以后创建的莫名其妙的错误。



我希望,当一个宽度或高度改变布局不立即执行,而是在系统重新检查尺寸为周期性变化


< DIV CLASS =h2_lin>解决方案

据该的 DispatherPriority枚举数据绑定发生前渲染




  • 发送

  • 正常 - 构造跑这里

  • 的DataBind

  • 渲染

  • 加载

  • 背景

  • ContextIdle

  • ApplicationIdle

  • SystemIdle

  • 无效

  • 无效

  • 输入



因此,结合会尝试先评估渲染发生之前。



不过渲染可能会导致你的绑定更新,因此,如果渲染父面板会增加面板的宽度的过程中(例如,一个父面板放置在另一个面板自动延伸其孩子占用100%像或最后一个元素在 DockPanel中),那么它就会在所绑定的更新,并增加渲染周期中孩子的宽度。



的第二部分本SO回答也可以帮助你理解。注意#6了。




当创建一个窗口,并显示事件序列



按照要求,这里是WPF重大的事件序列中创建并显示一个
窗口时:




  1. 构造函数和getter /作为创建对象,包括PropertyChangedCallback,ValidationCallback等在
    的对象进行更新,并且从他们继承的任何对象setter方法被称为


  2. 由于每个元素被添加到视觉或逻辑树的Intialized事件被触发,这将导致样式和触发器是
    外的任何特定元素初始化创建应用你
    可以定义[注:在逻辑
    树不解雇叶初始化事件,如果没有PresentationSource(如窗口)在其根]


  3. 窗口和所有的非折叠视觉测量,这将导致在每个控制,这会导致额外的
    对象树的结构,包括更多的建设者和
    getter / setter方法的ApplyTemplate,都


  4. 窗口和所有的非折叠视觉排列


  5. 窗口及其后代(逻辑和视觉)收到一个Loaded事件


  6. 当他们第一套重试失败

  7. $ b的任何数据绑定$ b
  8. 窗口和它的后代将得到一个机会来呈现其内容的可视化




步骤1-2创建窗口时完成的,无论它是否是
所示。其它步骤一般不会发生,直到一个窗口为
所示,但如果手动触发他们可以更早发生。



So If I have a binding placed on width of a child object that links it to its parent's ActualWidth, what will happen?

My guess is that the parent measures how much width the child wants, the child tells the parent 0 width, then the parent is given actual space during arrange, and it attempts to give the child zero since the child didn't want any. Then the actual width of the space given to the parent should cause the binding to change the child's width. At this point I would guess layout is performed again.

However, this assumes that the binding doesn't propagate that fast. I'm still hazy on when the bound value propagates to the target. It all depends on when the parent's actualwidth value changes. Does this occur after layout finishes? and then the bound pieces update? Does every binding interrupt the currently running code to go update the target value? if not, wouldn't it cause problems if one binding propagates a change that requires a redraw, then another binding propagates a different change that causes redraw, etc.

Some people asked what my actual problem was:

So originally I wanted to have a control stretch to fill the available space. Simple enough, but I wanted to have it be in a scrollviewer. Scrollviewer gives infinite space to its children during measure. So instead you can bind the width and height of the child control to the parent's actualwidth and actual height; layout does a second pass, and all seems swell.

However, later I was having issues of a similar kind stretching a control in a controltemplate but then found out that I can set a minwidth and alignment=stretch to have it stretch.

However, I distinctly remembered trying that earlier on my other control and having it not work, so I went back and tried to figure out what the difference between the two cases was. Basically it boiled down to one of them being in a stackpanel a few levels up.

So now I am using a binding for one and the minwidth plus alignment method for the other. Anyway was only interested in this to make sure that the way i'm doing things doesn't create weird bugs later.

I am hoping that layout doesn't run immediately when a width or height is changed but instead the system rechecks for size changes periodically

解决方案

According to the DispatherPriority enum, DataBinding occurs before Rendering.

  • Send
  • Normal - Constructors run here
  • DataBind
  • Render
  • Loaded
  • Background
  • ContextIdle
  • ApplicationIdle
  • SystemIdle
  • Inactive
  • Invalid
  • Input

So the binding would try to evaluate first before the render occurs.

However rendering can cause your binding to update, so if the process of rendering the parent panel would increase the width of the panel (for example, a parent panel placed inside of another panel that automatically stretches its children to take up 100% of the space like a Grid or last element in a DockPanel), then it will trigger an update on the binding and increase the width of the child during the render cycle.

The 2nd part of this SO answer may also help you understand. Take note of #6 too.

Sequence of events when a Window is created and shown

As requested, here is the sequence of major events in WPF when a window is created and shown:

  1. Constructors and getters/setters are called as objects are created, including PropertyChangedCallback, ValidationCallback, etc on the objects being updated and any objects that inherit from them

  2. As each element gets added to a visual or logical tree its Intialized event is fired, which causes Styles and Triggers to be found applied in addition to any element-specific initialization you may define [note: Initialized event not fired for leaves in a logical tree if there is no PresentationSource (eg Window) at its root]

  3. The window and all non-collapsed Visuals on it are Measured, which causes an ApplyTemplate at each Control, which causes additional object tree construction including more constructors and getters/setters

  4. The window and all non-collapsed Visuals on it are Arranged

  5. The window and its descendants (both logical and visual) receive a Loaded event

  6. Any data bindings that failed when they were first set are retried

  7. The window and its descendants are given an opportunity to render their content visually

Steps 1-2 are done when the Window is created, whether or not it is shown. The other steps generally don't happen until a Window is shown, but they can happen earlier if triggered manually.

这篇关于当孩子的宽度势必父母的ActualWidth的WPF会发生什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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