wpf Wpf控制内容的大小?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1746431/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:12:55  来源:igfitidea点击:

Wpf control size to content?

wpfwpf-controlsautosize

提问by Shimmy Weitzhandler

How do I make a WPF control to change its size according the content in it?

如何使 WPF 控件根据其中的内容更改其大小?

回答by Reed Copsey

For most controls, you set its height and width to Autoin the XAML, and it will size to fit its content.

对于大多数控件,您Auto在 XAML中将其高度和宽度设置为,并且其大小将适合其内容。

In code, you set the width/height to double.NaN. For details, see FrameworkElement.Width, particularly the "remarks" section.

在代码中,您将宽度/高度设置为double.NaN. 有关详细信息,请参阅FrameworkElement.Width,尤其是“备注”部分。

回答by TabbyCool

I had a problem like this whereby I had specified the width of my Window, but had the height set to Auto. The child DockPanelhad it's VerticalAlignmentset to Top and the Window had it's VerticalContentAlignment set to Top, yet the Window would still be much taller than the contents.

我遇到了这样的问题,即我指定了 Window 的宽度,但将高度设置为Auto. 孩子DockPanel将其VerticalAlignment设置为顶部,而窗口将其 VerticalContentAlignment 设置为顶部,但窗口仍会比内容高得多。

Using Snoop, I discovered that the ContentPresenterwithin the Window (part of the Window, not something I had put there) has it's VerticalAlignmentset to Stretchand can't be changed without retemplating the entire Window!

使用 Snoop,我发现ContentPresenter窗口内(窗口的一部分,不是我放在那里的东西)已经VerticalAlignment设置为Stretch并且不能在不重新设计整个窗口的情况下更改!

After a lot of frustration, I discovered the SizeToContentproperty - you can use this to specify whether you want the Window to size vertically, horizontally or both, according to the size of the contents - everything is sizing nicely now, I just can't believe it took me so long to find that property!

在经历了很多挫折之后,我发现了这个SizeToContent属性 - 您可以使用它来指定您是否希望窗口根据内容的大小进行垂直、水平或两者的调整 - 现在一切都很好,我简直不敢相信我花了很长时间才找到那处房产!

回答by ΩmegaMan

I had a user control which sat on page in a free form way, not constrained by another container, and the contents within the user control would not auto size but expand to the full size of what the user control was handed.

我有一个用户控件,它以自由形式位于页面上,不受另一个容器的约束,并且用户控件中的内容不会自动调整大小,而是扩展到用户控件的完整大小。

To get the user control to simply size to its content, for height only, I placed it into a grid with on row set to auto size such as this:

为了让用户控件简单地调整其内容的大小,仅针对高度,我将其放入一个网格中,并将行设置为自动大小,如下所示:

<Grid Margin="0,60,10,200">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <controls1:HelpPanel x:Name="HelpInfoPanel"
                         Visibility="Visible"
                         Width="570"
                         HorizontalAlignment="Right"
                         ItemsSource="{Binding HelpItems}"
                         Background="#FF313131" />
</Grid>

回答by Amadeusz Wieczorek

If you are using the grid or alike component: In XAML, make sure that the elements in the grid have Grid.Row and Grid.Column defined, and ensure tha they don't have margins. If you used designer mode, or Expression Blend, it could have assigned margins relative to the whole grid instead of to particular cells. As for cell sizing, I add an extra cell that fills up the rest of the space:

如果您使用网格或类似组件:在 XAML 中,确保网格中的元素定义了 Grid.Row 和 Grid.Column,并确保它们没有边距。如果您使用设计器模式或 Expression Blend,它可能会分配相对于整个网格而不是特定单元格的边距。至于单元格大小,我添加了一个额外的单元格来填充其余空间:

    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>