这是我的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>
边框设置在整个窗口周围。而且,当我创建另一个StackPanel时,它会添加到我之前的StackPanel的右侧,而不是添加到它下面。这是什么原因?
答案 0 :(得分:28)
这个怎么样:
<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>
答案 1 :(得分:16)
您将DockPanel.Dock =“Top”设置为StackPanel,但StackPanel不是DockPanel的子项... Border是。你的对接属性被忽略了。
如果您将DockPanel.Dock =“Top”移至边框,则会解决您的两个问题:)
答案 2 :(得分:0)
也许会有所帮助:
<Border BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Left" Height="160" Margin="10,55,0,0" VerticalAlignment="Top" Width="492"/>