简体   繁体   English

WPF-减少堆栈面板中项目之间的间隙

[英]WPF - reduce the gaps between the items in a stackpanel

I have a popup and it contains a stackpanel. 我有一个弹出窗口,它包含一个堆栈面板。 And there are some controls in the stackpanel. 而且堆栈面板中有一些控件。

   <Popup Name="popTest" Placement="Mouse" AllowsTransparency="True"     AllowDrop="True" PopupAnimation="Slide" Height="400" Width="500" >

        <Border BorderThickness="5" >
            <StackPanel Background="WhiteSmoke">
                <TextBlock Text="SomeName"  MaxWidth="100" MaxHeight="100" />
                <Image Source="{Binding TestImage, Mode=TwoWay}" Width="300" Height="300" />
                <TextBlock Text="TheOtherName" Margin="0,2,0,0" />
                <TextBlock Text="AnotherName"  TextWrapping="Wrap"  />
            </StackPanel>
        </Border>

       </Popup>

There is a considerbale gap between the items in the stackpanel. 堆栈面板中的各个项目之间存在一个大的空白。 I tried using Separator . 我尝试使用Separator It did not help me to reduce the gaps. 它没有帮助我缩小差距。 I even tried setting the widths and heights. 我什至尝试设置宽度和高度。 That too did not work. 那也没有用。

Is there any other way to do this ? 还有其他方法吗?

Remove MaxWidth="100" MaxHeight="100" from TextBlock. 从TextBlock中删除MaxWidth =“ 100” MaxHeight =“ 100”。 For Image Width="300" Height="300", does the width and height of image you load are 300 and 300, if image is small obviously you see empty space. 对于Image Width =“ 300” Height =“ 300”,加载的图像的宽度和高度是否分别为300和300,如果图像很小,显然您会看到空白。 You can use a Grid instead of stackpanel. 您可以使用网格而不是stackpanel。

For the image, if the actual aspect ratio of the original resource image is 3 to 2 and you specify Width="300" Height="200" the image will be displayed without spacing between it and the surround text blocks above and below. 对于图像,如果原始资源图像的实际纵横比为3到2,并且您指定Width="300" Height="200" ,则将显示图像,并且该图像与上方和下方的周围文本块之间不留空格。

If you want to guarantee no padding around such an image, either specify the Height , or the Width , but not both. 如果要保证在此类图像周围没有填充,请指定HeightWidth ,但不要同时指定两者。

Alternatively, if you want to force the image to be displayed as 300 x 200 (even if it is physically larger or of a different aspect ration) fill it to the size you want by setting the Stretch attribute:- 或者,如果您想强制将图像显示为300 x 200(即使它在物理上更大或具有不同的宽高比),则可以通过设置Stretch属性将其填充为所需的尺寸:-

<Image Source="TestImage.jpg" Width="300" Height="200" Stretch="Fill"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM