c# - WPF 当子级的宽度绑定(bind)到父级的实际宽度时会发生什么

标签 c# wpf xaml actualwidth

因此,如果我在子对象的宽度上放置了一个绑定(bind),将其链接到其父对象的 ActualWidth,会发生什么?

我的猜测是,父级测量子级想要多少宽度,子级告诉父级 0 宽度,然后在排列期间为父级提供实际空间,并且它尝试给子级零,因为子级不想要任何。然后,给予父级的空间的实际宽度应该导致绑定(bind)改变子级的宽度。此时我猜测布局会再次执行。

但是,这假设绑定(bind)传播的速度没有那么快。我仍然不清楚绑定(bind)值何时传播到目标。这完全取决于父级的实际宽度值何时发生变化。布局完成后会发生这种情况吗?然后绑定(bind)的碎片会更新吗?每个绑定(bind)都会中断当前正在运行的代码以更新目标值吗?如果不是,如果一个绑定(bind)传播需要重绘的更改,然后另一个绑定(bind)传播导致重绘的不同更改,等等,是否会导致问题。

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

所以最初我想要一个控制拉伸(stretch)来填充可用空间。很简单,但我想把它放在滚动查看器中。 Scrollviewer 在测量期间为其子级提供了无限的空间。因此,您可以将子控件的宽度和高度绑定(bind)到父控件的实际宽度和实际高度;布局进行了第二遍,一切看起来都很好。

但是,后来我遇到了类似的拉伸(stretch)控件模板中的控件的问题,但后来发现我可以设置 minwidth 和alignment=stretch 来拉伸(stretch)它。

但是,我清楚地记得早些时候在我的另一个控件上尝试过,但它不起作用,所以我回去并试图找出这两种情况之间的区别。基本上,它可以归结为其中一个位于更高几层的堆栈面板中。

所以现在我对一个使用绑定(bind),对另一个使用 minwidth 加对齐方法。 不管怎样,我只是对此感兴趣,以确保我做事的方式不会在以后产生奇怪的错误。

我希望布局在宽度或高度更改时不会立即运行,而是系统定期重新检查尺寸更改

最佳答案

根据DispatherPriority enumDataBinding 发生在Rendering 之前。

  • 发送
  • 正常 - 构造函数在这里运行
  • 数据绑定(bind)
  • 渲染
  • 已加载
  • 背景
  • 上下文空闲
  • 应用程序空闲
  • 系统空闲
  • 不活动
  • 无效
  • 输入

因此,绑定(bind)将在渲染发生之前尝试首先进行评估。

但是渲染可能会导致您的绑定(bind)更新,因此如果渲染父面板的过程会增加面板的宽度(例如,父面板放置在另一个面板内部,该面板会自动拉伸(stretch)其子面板以占据 100%空间(例如 GridDockPanel 中的最后一个元素),那么它将触发绑定(bind)更新并在渲染周期期间增加子项的宽度。

this SO answer的第二部分或许也能帮助你理解。还要注意#6。

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.

关于c# - WPF 当子级的宽度绑定(bind)到父级的实际宽度时会发生什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17604072/

相关文章:

c# - XAML 或 C# 代码隐藏

c# - 从cs文件渲染html到cshtml

如果第二次启动,C# 程序只能运行一次 + BringToFront

c# - WPF UI 权限,在数据库中定义

wpf - InputBindings 仅在聚焦时才起作用

c# - 需要解雇员工的能力

c# - 获取动态添加的子控件以显示在 UI 中

c# - 无法建立 SSL 连接,请参阅内部异常

WPF:路由事件还是常规事件?

xaml - 打开日期选择器日历,而不在xamarin.forms中显示edittextbox