Force a custom WPF Control to resize correctly(强制自定义 WPF 控件正确调整大小)
问题描述
我已经编写了一个 WPF 用户控件,其中一部分涉及向画布动态添加元素,从而影响所述画布的高度.画布嵌套在网格中.当我动态添加我的元素时,画布的高度会发生变化,但画布最终会延伸到整个控件的边缘之外,而不是导致控件调整大小并使其自身更高.如何强制控件正确调整大小?我觉得我需要调用或覆盖 Measure 或 Arrange 但我对这两种方法都没有运气 - 可能是因为我使用不正确的参数调用它们,或者可能是因为它们不是正确的调用方法.
I have written a WPF user control and part of it involves dynamically adding elements to a canvas which effects the height of said canvas. The canvas is nested within a grid. When I dynamically add my elements the height of the canvas changes, but the canvas ends up extending beyond the edge of the overall control rather than causing the control to resize and make itself taller. How can I force the control to resize correctly? I get the feeling I need to either call or override Measure or Arrange but I’m having no luck with either method – possibly because I’m calling them with incorrect parameters or possibly because they aren’t the correct methods to be calling.
推荐答案
您的问题是 Canvas 面板无法调整大小以适应其内容.
Your problem is that Canvas panels do not resize to fit their content.
您提到了测量和排列.你熟悉 WPF 的两遍布局系统吗?阅读本文了解更多信息:WPF 的布局系统.
You refered to Measure and Arrange. Are you familiar with WPF's two pass layout system? Read this article for more information: WPF's Layout System.
本文还介绍了不同面板对布局的作用.每种类型的面板都是不同的.
This article also describes what different panels do for layout. Each type of panel is different.
在 WPF 中,元素首先有机会确定它们的大小.小组可以根据孩子的大小来确定他们的大小,或者他们可以要求固定大小而不管孩子的大小.帆布就是后者的一个例子.
In WPF, elements are first given an opportunity to determine what size they will be. Panels can determine their size based on the size of their children, or they can ask for fixed size regardless of the size of their children. Canvas is an example of the latter.
接下来会告诉元素它们的大小,并要求它们排列它们自己和它们的子元素.
Elements are next told what size they will be and asked to arrange themselves and their children.
Grid 面板类型不仅允许行和列,而且非常擅长根据内容自动调整大小.
The Grid panel type not only allows rows and columns, but is also very good at auto sizing to content.
StackPanel 类型在它所面向的维度中根本不会自动调整大小,但会在其他维度中自动调整大小.
The StackPanel type does not autosize at all in the dimension in which it is oriented, but it will autosize in the other dimension.
Canvas 可以自动调整大小以填充空间,但绝不允许其子级自动调整大小.它在指定的坐标处绘制它的孩子,而不关心它们有多大.
Canvas can auto size to fill a space, but it never allows it's children to auto size. It draws its children at the specified coordinates and does not care how big they are.
我建议尝试只有一行/列的网格.
I would suggest trying a Grid with only one row/column.
或者您可以创建一个具有更好自动调整大小功能的新 Canvas 类.在测量期间,您需要测量子元素并相应地调整 Canvas 的大小.您可能会发现这样的课程对您很有帮助.
Or you could create a new Canvas class that has better autosizing capabilities. During measure, you would want to measure the child elements and size your Canvas accordingly. You might find a class like this helpful down the road.
这是一篇关于 WPF 中自动布局的文章.
Here's an article on Auto Layout in WPF.
如果您想创建自定义面板,这里有一个子主题:自定义面板元素.
If you want to create a custom panel, here's a subtopic on it: Custom Panel Elements.
还有一件事:您还可以将子元素的宽度/高度绑定到画布上的 ActualWidth 和 ActualHeight 属性,以便子元素调整其父元素的大小.如有必要,您可以使用转换器设置大小比例.
One more thing: You can also bind the width/height of the child element to the ActualWidth and ActualHeight properties on the canvas so that the child will adjust the size of its parent. You can use a converter to set a size ratio if necessary.
这篇关于强制自定义 WPF 控件正确调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:强制自定义 WPF 控件正确调整大小
基础教程推荐
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 如何激活MC67中的红灯 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01