wpf 如何使 StackPanel 适合其孩子的宽度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18985422/
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-13 09:43:17  来源:igfitidea点击:

How to make a StackPanel fit its Children's width

c#wpfxaml

提问by Vereos

I'm writing down here because I'm having lots of issues with this thing, and I hope you will be able to help me out :P

我写在这里是因为我在这件事上有很多问题,我希望你能帮助我:P

I want to create a dynamic StackPanel that fits all its Children's width. I'll make you an example.

我想创建一个适合其所有儿童宽度的动态 StackPanel。我给你举个例子。

Let's say my StackPanel is currently empty. Its Width will be 10 (f.e.) and its fixed Height is 30. Then I want to add an Image, and I wrote this down:

假设我的 StackPanel 当前为空。它的 Width 为 10 (fe),固定 Height 为 30。然后我想添加一个 Image,我写下了这个:

BitmapImage myImage = new BitmapImage(new Uri("[PATH]"));
Image realImage = new Image();
realImage.Source = myImage;
realImage.Width = 50;
realImage.Height = myImage.Height;
myStackPanel.Children.Add(realImage);

The StackPanel does not enlarge to 50px though; it's stuck to 10px, cutting my image. the same happens when I try to add other UI Elements, like TextBlocks and so on.

但是 StackPanel 没有放大到 50px;它坚持 10px,削减我的形象。当我尝试添加其他 UI 元素(如 TextBlocks 等)时,也会发生同样的情况。

I tried to do something like

我试着做类似的事情

myStackPanel.Width += realImage.Width;

But of course it didn't work. I've tried to set the StackPanel's Width to "Auto", but it didn't help either. This is the StackPanel declaration in the XAML:

但当然没有用。我试图将 StackPanel 的宽度设置为“自动”,但也无济于事。这是 XAML 中的 StackPanel 声明:

<Window x:Name="MainWindow1" x:Class="Proj.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="myTitle" Height="480" Width="640"
    Loaded="onLoad" Background="Black" MinHeight="480" MinWidth="640">
    <Grid>
        ...(Other Closed Tags)...
        <StackPanel Orientation="Horizontal" Name="myStackPanel" Height="30" Margin="135,283,458,147"/>
    </Grid>
</Window>

I need a StackPanel because I have to show a dynamic bar with a variable number of objects that will perform a marquee effect on the screen, sliding from right to left. I don't know neither the number of the objects nor the final width of my dynamic bar.

我需要一个 StackPanel,因为我必须显示一个动态条,其中包含可变数量的对象,这些对象将在屏幕上从右向左滑动,产生选取框效果。我既不知道对象的数量,也不知道动态条的最终宽度。

Of course, if you know there is an other way to do it, I'll be glad to learn something about it :)

当然,如果您知道还有其他方法可以做到,我会很高兴了解它:)

Thank you!

谢谢!

回答by Chevul Ervin

Put your stackpanel into a ScrollViewer and set its width to Auto:

将您的堆栈面板放入 ScrollViewer 并将其宽度设置为 Auto:

    <ScrollViewer HorizontalScrollBarVisibility="Auto">
        <StackPanel  Width="Auto">
            ....
        </StackPanel>
    </ScrollViewer>

回答by Charleh

I've just checked and yes, your problem is Margin. Do NOT use Marginfor absolute positioning in layouts. Marginshould be used as a margin offset from the parent control, but should be avoided when trying to position elements (such as placing an item in the centre of a form) as it clips the elements to a particular bounds.

我刚刚检查过,是的,您的问题是Margin。不要Margin用于布局中的绝对定位。Margin应该用作与父控件的边距偏移量,但在尝试定位元素(例如将项目放置在表单的中心)时应避免使用,因为它将元素剪辑到特定边界。

The effect you want is easily achieved with an autowidth StackPanel

用一个auto宽度轻松实现你想要的效果StackPanel

<StackPanel Orientation="Horizontal" Name="myStackPanel" Height="30">
        <Button>Hello</Button>
        <Button>World</Button>
</StackPanel>

Obviously - if you need to position this in a particular place in your window, you need to ensure that it has the full container width available - use HorizontalAlignmentand Gridto place your controls

显然 - 如果您需要将其放置在窗口中的特定位置,您需要确保它具有可用的完整容器宽度 - 使用HorizontalAlignmentGrid放置您的控件

From your layout I'd suggest something more like this:

根据您的布局,我建议更像这样:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="1.4*"></RowDefinition>
        <RowDefinition Height="30"></RowDefinition>
        <RowDefinition Height="*"></RowDefinition>
    </Grid.RowDefinitions>
    <StackPanel Orientation="Horizontal" Grid.Row="1" Name="myStackPanel">
        <Button>Hello</Button>
        <Button>World</Button>
    </StackPanel>
</Grid>

I'd avoid giving elements explicit sizes unless absolutely necessary - if your parent container can dictate the size, then all the better

除非绝对必要,否则我会避免为元素提供明确的大小 - 如果您的父容器可以决定大小,那就更好了

If you want the images horizontally aligned within the StackPanel, you can set HorizontalAlignmentto Center

如果您希望图像在 内水平对齐StackPanel,您可以设置HorizontalAlignmentCenter