wpf 在 StackPanel 周围设置边框。

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

Set a border around a StackPanel.

wpforientationstackpanel

提问by

Here's my XAML code:

这是我的 XAML 代码:

<Window x:Class="CarFinder.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Search for cars in TuMomo" Height="480" Width="600">
    <DockPanel Margin="8">
        <Border CornerRadius="6"
                BorderBrush="Gray"
                Background="LightGray"
                BorderThickness="2"
                Padding="8">
            <StackPanel Orientation="Horizontal"
                    DockPanel.Dock="Top"
                    Height="25">
                <TextBlock FontSize="14" Padding="0 0 8 0">
                    Search:
                </TextBlock>
                <TextBox x:Name="txtSearchTerm" Width="400" />
                <Image Source="/CarFinder;component/Images/Chrysanthemum.jpg" />            
            </StackPanel>
        </Border>
        <StackPanel Orientation="Horizontal"
                    DockPanel.Dock="Top"
                    Height="25">

        </StackPanel>
    </DockPanel>
</Window>

The border is set around the entire window. And also, when I create another StackPanel it's added to the right of my previous StackPanel instead of being added under it. What's the reason for this?

边框设置在整个窗口周围。而且,当我创建另一个 StackPanel 时,它会添加到我之前的 StackPanel 的右侧,而不是添加到它下面。这是什么原因?

采纳答案by Brian Genisio

You set DockPanel.Dock="Top" to the StackPanel, but the StackPanel is not a child of the DockPanel... the Border is. Your docking property is being ignored.

您将 DockPanel.Dock="Top" 设置为 StackPanel,但 StackPanel 不是 DockPanel 的子项... Border 是。您的停靠属性被忽略。

If you move DockPanel.Dock="Top" to the Border instead, both of your problems will be fixed :)

如果您将 DockPanel.Dock="Top" 移动到 Border,您的两个问题都将得到解决:)

回答by Mohammad Dayyan

What about this one :

这个如何 :

<DockPanel Margin="8">
    <Border CornerRadius="6" BorderBrush="Gray" Background="LightGray" BorderThickness="2" DockPanel.Dock="Top">
        <StackPanel Orientation="Horizontal">
            <TextBlock FontSize="14" Padding="0 0 8 0" HorizontalAlignment="Center" VerticalAlignment="Center">Search:</TextBlock>
            <TextBox x:Name="txtSearchTerm" HorizontalAlignment="Center" VerticalAlignment="Center" />
            <Image Source="lock.png" Width="32" Height="32" HorizontalAlignment="Center" VerticalAlignment="Center" />            
        </StackPanel>
    </Border>
    <StackPanel Orientation="Horizontal" DockPanel.Dock="Bottom" Height="25" />
</DockPanel>

回答by Jenish Titus

May be it will helpful:

可能会有所帮助:

<Border BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Left" Height="160" Margin="10,55,0,0" VerticalAlignment="Top" Width="492"/>