gpt4 book ai didi

wpf - 是否可以强制窗口的大小永远不会低于其子项所需的大小?

转载 作者:行者123 更新时间:2023-12-05 06:31:13 24 4
gpt4 key购买 nike

我知道对于 Window(实际上是任何 FrameworkElement),您可以指定 MinWidthMinHeight。我想知道的是如何使最小匹配窗口的 DesiredSize

换句话说,我想说“您可以根据需要调整此窗口的大小,但不能小于显示其内容所需的最小值。”

例如,假设我有一个简单的 Window,其中包含一个 DockPanel,其中三个项目垂直排列。为简单起见,我们只讨论高度。

  1. 第一个是一个 RectangleHeight 为 20,固定在顶部,因此其 DesiredSize.Height 为 20 .

  2. 第二个是 Label,它的 MinHeight 为 20,固定在底部,但如果有多行文本,高度可以增加. (目前,只有一个单词,所以 DesiredSize.Height 也是 20)

  3. 第三项是自定义 ImageViewer 控件,它填充中间的剩余区域,但绝不会小于其内容加上填充。当前图像为 15x15,边距为 2.5,因此其 DesiredSize.Height 为 20 (2.5 + 15 + 2.5)

根据以上内容,StackPanel 的 DesiredSize.Height 将为 60 (20 + 20 + 20),而 DesiredSize.Height Window 将是 60 + 它自己的 chrome 高度(假设它也是 20),这似乎给了我一个 80 的 Window.DesiredSize.Height。这就是我的m 之后为窗口的 MinHeight。

我尝试通过 SizeToDoubleConverterWindowMinHeight 绑定(bind)到它的 DesiredSize.Height (它需要一个 Dimension 来确定相应地返回 WidthHeight)但它没有任何效果。

这看起来很有希望......

protected override Size MeasureOverride(Size availableSize) {

var desiredSize = base.MeasureOverride(availableSize);

MinWidth = Math.Ceiling(DesiredSize.Width);
MinHeight = Math.Ceiling(DesiredSize.Height);

return desiredSize;
}

注意:我倾向于在实际的 DesiredSize.Height 上使用 Math.Ceiling,因为它通常是一个分数,并且在对齐到设备像素,搞砸了。

上面的代码接近于工作,但是如果您还想将 MaxHeight 设置为等于它(例如,允许水平调整,但不允许垂直调整大小),则会遇到问题。

这种事情在 iOS/macOS 中使用 AutoLayout 很容易,但我不确定如何在 WPF 中做到这一点,即使是以编程方式。我只需要不断猜测什么尺寸看起来最好。

最佳答案

这是个好问题。我已经实现了目标,但它并不漂亮。我不推荐这种方法,但它确实如其所言。

基本框架,名为 DynamicMinumumSize 的项目:

主窗口.xaml.cs:

public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();

DataContext = new BackingData();
}
}

public class BackingData
{
public double StackMinHeight { get; set; }
}

Xaml:

<Window x:Class="DynamicMinimumSize.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:DynamicMinimumSize"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525" d:DataContext="{d:DesignInstance local:BackingData}" MinHeight="{Binding ElementName=Stack, Path=MinHeight}">
<Window.Resources>
<ResourceDictionary>
<local:ChildMinHeightConverter x:Key="ChildMinHeightConverter" />
</ResourceDictionary>
</Window.Resources>
<StackPanel x:Name="Stack">
<StackPanel.MinHeight>
<MultiBinding Converter="{StaticResource ChildMinHeightConverter}" >
<MultiBinding.Bindings>
<Binding Path="StackMinHeight" />
<Binding RelativeSource="{RelativeSource Self}"/>
</MultiBinding.Bindings>
</MultiBinding>
</StackPanel.MinHeight>
<Button MinHeight="53">Min Height 53</Button>
<Button MinHeight="53">Min Height 53</Button>
<Button MinHeight="53">Min Height 53</Button>
</StackPanel>
</Window>

转换器:

public class ChildMinHeightConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
var currentMinHeight = values[0] as double? ?? 0;

if (values[1] is Panel)
{
var parent = values[1] as Panel;
foreach (var child in parent.Children)
{
//Hack: The children won't necessarily be buttons
currentMinHeight += (child as Button).MinHeight;
}
}
//HACK: +40, I think it's the window toolbar.....
return currentMinHeight + 40;
}

public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}

可视化:

enter image description here

关于wpf - 是否可以强制窗口的大小永远不会低于其子项所需的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51871967/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com