- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我试图通过重写Panel
和MeasureOverride
为WPF编写一个自定义ArrangeOverride
类,但是尽管它在大多数情况下都在工作,但我遇到了一个我无法解释的奇怪问题。
特别是,在弄清楚子项的大小后,我在Arrange
的子项上调用ArrangeOverride
后,它们的大小没有达到我给它们的大小,并且似乎已调整为Measure
中传递给其MeasureOverride
方法的大小。
我在该系统应该如何工作方面缺少什么吗?我的理解是,调用Measure
只会使 child 根据所提供的availableSize评估其DesiredSize
,而不会影响其实际最终尺寸。
这是我的完整代码(Panel,btw,旨在以最节省空间的方式排列子级,为不需要它的行提供较少的空间,并将剩余空间平均分配给其余的空间,目前仅支持垂直方向,但我打算在水平方向正常工作后再添加水平方向):
编辑:感谢您的答复。一会儿我将更仔细地研究它们。但是,让我澄清一下我预期的算法是如何工作的,因为我没有对此进行解释。
首先,思考我在做什么的最好方法是想象一个将每行设置为*的网格。这样可以将空间平均分配。但是,在某些情况下,行中的元素可能并不需要所有的空间。如果是这种情况,我想获取任何剩余的空间,并将其分配给可以使用该空间的那些行。如果没有行需要任何额外的空间,我只是尝试均匀地隔开空间(这就是extraSpace
所做的,仅适用于这种情况)。
我分两次通过。第一遍的最终目的是确定一行的最终“正常大小”,即将缩小的行的大小(给定的大小小于其期望的大小)。我这样做是通过将最小的项目逐步放大到最大,然后在每个步骤中调整计算得出的正常大小,方法是将每个小项目的剩余空间添加到随后的每个大项目中,直到没有其他项目“适合”然后折断。
在下一个过程中,我将使用此正常值来确定某个项目是否适合,只需简单地将正常大小的Min
与该项目的所需大小一起使用即可。
(为简单起见,我还将匿名方法更改为lambda函数。)
编辑2:我的算法似乎可以很好地确定 child 的适当大小。但是, children 只是不接受他们给定的尺寸。我通过传递PositiveInfinity并返回Size(0,0)来尝试了哥布林建议的MeasureOverride
,但这使子级绘制自己,好像根本没有空间限制。对此不明显的部分是由于对Measure
的调用而发生的。微软在这个主题上的文档还不清楚,因为我已经多次阅读了每个类和属性的描述。但是,现在很清楚,调用Measure
实际上确实会影响子代的呈现,因此我将尝试按照BladeWise的建议在这两个函数之间进行逻辑划分。
解决了! 我知道了。正如我所怀疑的,我需要对每个 child 两次调用Measure()(一次评估DesiredSize,第二次给每个 child 适当的高度)。对我来说,WPF中的布局将以这种奇怪的方式设计似乎很奇怪,在该布局中它分为两遍,但Measure遍实际上做了两件事:度量和确定子级的大小,而Arrange遍除了实际物理上几乎什么也没做安置 children 。很奇怪
我将工作代码发布在底部。
首先,原始(残破)代码:
protected override Size MeasureOverride( Size availableSize ) {
foreach ( UIElement child in Children )
child.Measure( availableSize );
return availableSize;
}
protected override System.Windows.Size ArrangeOverride( System.Windows.Size finalSize ) {
double extraSpace = 0.0;
var sortedChildren = Children.Cast<UIElement>().OrderBy<UIElement, double>( child=>child.DesiredSize.Height; );
double remainingSpace = finalSize.Height;
double normalSpace = 0.0;
int remainingChildren = Children.Count;
foreach ( UIElement child in sortedChildren ) {
normalSpace = remainingSpace / remainingChildren;
if ( child.DesiredSize.Height < normalSpace ) // if == there would be no point continuing as there would be no remaining space
remainingSpace -= child.DesiredSize.Height;
else {
remainingSpace = 0;
break;
}
remainingChildren--;
}
// this is only for cases where every child item fits (i.e. the above loop terminates normally):
extraSpace = remainingSpace / Children.Count;
double offset = 0.0;
foreach ( UIElement child in Children ) {
//child.Measure( new Size( finalSize.Width, normalSpace ) );
double value = Math.Min( child.DesiredSize.Height, normalSpace ) + extraSpace;
child.Arrange( new Rect( 0, offset, finalSize.Width, value ) );
offset += value;
}
return finalSize;
}
double _normalSpace = 0.0;
double _extraSpace = 0.0;
protected override Size MeasureOverride( Size availableSize ) {
// first pass to evaluate DesiredSize given available size:
foreach ( UIElement child in Children )
child.Measure( availableSize );
// now determine the "normal" size:
var sortedChildren = Children.Cast<UIElement>().OrderBy<UIElement, double>( child => child.DesiredSize.Height );
double remainingSpace = availableSize.Height;
int remainingChildren = Children.Count;
foreach ( UIElement child in sortedChildren ) {
_normalSpace = remainingSpace / remainingChildren;
if ( child.DesiredSize.Height < _normalSpace ) // if == there would be no point continuing as there would be no remaining space
remainingSpace -= child.DesiredSize.Height;
else {
remainingSpace = 0;
break;
}
remainingChildren--;
}
// there will be extra space if every child fits and the above loop terminates normally:
_extraSpace = remainingSpace / Children.Count; // divide the remaining space up evenly among all children
// second pass to give each child its proper available size:
foreach ( UIElement child in Children )
child.Measure( new Size( availableSize.Width, _normalSpace ) );
return availableSize;
}
protected override System.Windows.Size ArrangeOverride( System.Windows.Size finalSize ) {
double offset = 0.0;
foreach ( UIElement child in Children ) {
double value = Math.Min( child.DesiredSize.Height, _normalSpace ) + _extraSpace;
child.Arrange( new Rect( 0, offset, finalSize.Width, value ) );
offset += value;
}
return finalSize;
}
Measure
(并重复三次
Children
)可能不是很高效,但是它可以工作。对算法的任何优化将不胜感激。
最佳答案
让我们看看我是否正确,Panel
应该如何工作:
UIElement
子UIElement
的大小,以便填充整个空间(即,每个元素的大小将增加剩余空间的一部分)UIElement
)的情况下,Measure传递用于确定
availableSize
需要多少空间。在
Panel
的情况下,它也会对其子级调用Measure传递,但是
并未设置其子级的所需大小(换句话说,子级的大小是面板的测量传递的输入)。
Panel
的情况下,它也会在其子级上调用Arrange传递,但是就像小节传递一样,
不会更改子级的所需大小(它只会定义其渲染空间)。
AttachedProperty
(即RequiredHeight)代替所需的子项大小(除非将子项大小设置为Auto
,否则您无法控制子项大小,因此无需采取DesiredSize
)using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;
namespace WpfApplication1
{
public class CustomPanel : Panel
{
/// <summary>
/// RequiredHeight Attached Dependency Property
/// </summary>
public static readonly DependencyProperty RequiredHeightProperty = DependencyProperty.RegisterAttached("RequiredHeight", typeof(double), typeof(CustomPanel), new FrameworkPropertyMetadata((double)double.NaN, FrameworkPropertyMetadataOptions.AffectsMeasure, new PropertyChangedCallback(OnRequiredHeightPropertyChanged)));
private static void OnRequiredHeightPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
{
}
public static double GetRequiredHeight(DependencyObject d)
{
return (double)d.GetValue(RequiredHeightProperty);
}
public static void SetRequiredHeight(DependencyObject d, double value)
{
d.SetValue(RequiredHeightProperty, value);
}
private double m_ExtraSpace = 0;
private double m_NormalSpace = 0;
protected override Size MeasureOverride(Size availableSize)
{
//Measure the children...
foreach (UIElement child in Children)
child.Measure(availableSize);
//Sort them depending on their desired size...
var sortedChildren = Children.Cast<UIElement>().OrderBy<UIElement, double>(new Func<UIElement, double>(delegate(UIElement child)
{
return GetRequiredHeight(child);
}));
//Compute remaining space...
double remainingSpace = availableSize.Height;
m_NormalSpace = 0.0;
int remainingChildren = Children.Count;
foreach (UIElement child in sortedChildren)
{
m_NormalSpace = remainingSpace / remainingChildren;
double height = GetRequiredHeight(child);
if (height < m_NormalSpace) // if == there would be no point continuing as there would be no remaining space
remainingSpace -= height;
else
{
remainingSpace = 0;
break;
}
remainingChildren--;
}
//Dtermine the extra space to add to every child...
m_ExtraSpace = remainingSpace / Children.Count;
return Size.Empty; //The panel should take all the available space...
}
protected override System.Windows.Size ArrangeOverride(System.Windows.Size finalSize)
{
double offset = 0.0;
foreach (UIElement child in Children)
{
double height = GetRequiredHeight(child);
double value = (double.IsNaN(height) ? m_NormalSpace : Math.Min(height, m_NormalSpace)) + m_ExtraSpace;
child.Arrange(new Rect(0, offset, finalSize.Width, value));
offset += value;
}
return finalSize; //The final size is the available size...
}
}
}
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
Title="MainWindow" Height="350" Width="525">
<Grid>
<local:CustomPanel>
<Rectangle Fill="Blue" local:CustomPanel.RequiredHeight="22"/>
<Rectangle Fill="Red" local:CustomPanel.RequiredHeight="70"/>
<Rectangle Fill="Green" local:CustomPanel.RequiredHeight="10"/>
<Rectangle Fill="Purple" local:CustomPanel.RequiredHeight="5"/>
<Rectangle Fill="Yellow" local:CustomPanel.RequiredHeight="42"/>
<Rectangle Fill="Orange" local:CustomPanel.RequiredHeight="41"/>
</local:CustomPanel>
</Grid>
</Window>
关于c# - 自定义自动调整大小的WPF面板类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3002656/
这个问题在这里已经有了答案: C sizeof a passed array [duplicate] (7 个回答) 8年前关闭。 在一个函数中,我声明了一个数组: int char_count_ar
简而言之,文件系统如何与 block 设备通信? 最佳答案 我对 block 大小不太了解。我认为 ext4(Linux)的文件系统的 block 大小是 4KB,考虑到现代处理器的页面大小(4KB)
我知道 tinyint(1) 和 tinyint(2) 具有相同的存储空间范围。 唯一的区别是显示宽度不同。这是否意味着 tinyint(1) 将存储所有类型的整数但只正确显示 0 到 9 的范围?而
今晚我已经研究了以下代码几个小时,但我只是摸不着头脑。 当使用函数从标准输入填充数组时,我不断收到“大小 8 的无效写入”和“大小 8 的无效读取”。 如有任何帮助,我们将不胜感激...我知道 Sta
我有一个 valgrind 错误,我不知道如何摆脱它们: ==5685== Invalid read of size 8 ==5685== at 0x4008A1: main (in /home
我对 Hadoop 的概念有点困惑。 Hadoop block 大小、拆分大小和 block 大小 之间有什么区别? 提前致谢。 最佳答案 block 大小和 block 大小相同。 拆分大小 可能与
我想不出一个好的标题,所以希望可以。 我正在做的是创建一个离线 HTML5 webapp。 “出于某些原因”我不希望将某些文件放在缓存 list 中,而是希望将内容放在 localStorage 中。
无法将 xamarin apk 大小减少到 80 MB 以下,已执行以下操作: 启用混淆器 配置:发布 平台:事件(任何 CPU)。 启用 Multi-Dex:true 启用开发人员检测(调试和分析)
我正在开发一个程序,需要将大量 csv 文件(数千个)加载到数组中。 csv 文件的尺寸为 45x100,我想创建一个尺寸为 nx45x100 的 3-d 数组。目前,我使用 pd.read_csv(
Hello World 示例的 React Native APK 大小约为 20M (in recent versions),因为支持不同的硬件架构(ARMv7、ARMv8、X86 等),而同一应用程
我有一个包含 n 个十进制元素的列表,其中每个元素都是两个字节长。 可以说: x = [9000 , 5000 , 2000 , 400] 这个想法是将每个元素拆分为 MSB 和 LSB 并将其存储在
如何设置 GtKTextView 的大小?我想我不能使用 gtk_widget_set_usize。 最佳答案 您不能直接控制小部件的大小,而是由其容器完成。您可以使用 gtk_widget_set_
这个问题在这里已经有了答案: c++ sizeof() of a class with functions (7 个答案) 关闭 5 年前。 结果是 12。 foobar 函数存储在内存中的什么位置
当我在 ffmpeg(或任何其他程序)中使用这样的命令时: ffmpeg -i input.mp4 image%d.jpg 所有图像的组合文件大小总是比视频本身大。我尝试减少每秒帧数、降低压缩设置、模
我是 clojurescript 的新手。 高级编译后出现“77 KB”的javascript文件是否正常? 我有一个 clojurescript 文件: 我正在使用 leinigen: lein c
我想要一个 QPixmap尺寸为 50 x 50。 我试过 : QPixmap watermark(QSize(50,50)); watermark.load(":/icoMenu/preparati
我正在尝试从一篇研究论文中重新创建一个 cnn,但我对深度学习还是个新手。 我得到了一个大小为 32x32x7 的 3d 补丁。我首先想执行一个大小为 3x3 的卷积,具有 32 个特征和步幅为 2。
我一直在尝试调整 View Controller 内的 View 大小,但到目前为止没有运气。基本上,我的 View 最底部有一个按钮,当方向从纵向更改为横向时,该按钮不再可见,因为它现在太靠下了。
如何使用此功能检查图像的尺寸?我只是想在上传之前检查一下... $("#LINK_UPLOAD_PHOTO").submit(function () { var form = $(this);
我用 C++ 完成了这个,因为你可以通过引用传递参数。我无法弄清楚如何在 JavaScript 中执行此操作。我的代码需要更改什么?我的输出是1 this.sizeOfBst = function()
我是一名优秀的程序员,十分优秀!