Set a border around a StackPanel.

WpfOrientationStackpanel

Wpf Problem Overview


Here's my XAML code:

<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?

Wpf Solutions


Solution 1 - Wpf

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>

Solution 2 - Wpf

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.

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

Solution 3 - Wpf

May be it will helpful:

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondeleteView Question on Stackoverflow
Solution 1 - WpfMohammad DayyanView Answer on Stackoverflow
Solution 2 - WpfBrian GenisioView Answer on Stackoverflow
Solution 3 - WpfJenish TitusView Answer on Stackoverflow