英文:
How i can make StackPanel with horizontal orientation, center align and streched?
问题
我需要水平方向的StackPanel,其中内容居中对齐。但是当我将HorizontalAlignment设置为'Center'时,StackPanel会缩小到内容(图像)。我需要Panel的所有区域来确定鼠标光标是否在其上。
我明白了 - StackPanel就像是一个队列,我们一个接一个地放置项目。但为什么它不能被拉伸并居中对齐呢?
我的XAML代码:
<Window x:Class="panels.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:panels"
mc:Ignorable="d"
Title="MainWindow" Height="600" Width="1000" MouseDown="Window_MouseDown" >
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<StackPanel Grid.Column="1" Grid.Row="0" Orientation="Horizontal" Background="#FF0019E8">
<Image Source="/icon.png" Height="200" HorizontalAlignment="Center"/>
</StackPanel>
</Grid>
</Window>
我可以将HorizontalAligment设置为Strech:变体1
我可以将HorizontalAligment设置为Center:变体2
但需要类似于这样的效果:变体3。
英文:
I need horizontal oriented StackPanel with centered aligned content. But when i set HorizontalAligment to 'Center', StackPanel shrink to content (Image). I need all area of panel for determination mouse cursor over it.
I understand - the StackPanel is like queue where we put items one after one. But why it cant be stretched and aligned by center?
My XAML code:
<Window x:Class="panels.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:panels"
mc:Ignorable="d"
Title="MainWindow" Height="600" Width="1000" MouseDown="Window_MouseDown" >
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<StackPanel Grid.Column="1" Grid.Row="0" Orientation="Horizontal" Background="#FF0019E8">
<Image Source="/icon.png" Height="200" HorizontalAlignment="Center"/>
</StackPanel>
</Grid>
</Window>
i can do HorizontalAligment=Strech: Variant 1
i can do HorizontalAligment=Center: Variant 2
but need something like this: Variant 3
答案1
得分: 2
要获得您想要的结果,可以将您的 StackPanel 包装在一个容器中(例如 Grid),该容器将被拉伸:
<Window x:Class="panels.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:panels"
mc:Ignorable="d"
Title="MainWindow" Height="600" Width="1000" MouseDown="Window_MouseDown">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Grid Grid.Column="1" Grid.Row="0" Background="#FF0019E8">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<Image Source="/icon.png" Height="200" HorizontalAlignment="Center"/>
</StackPanel>
</Grid>
</Grid>
</Window>
要处理的背景和鼠标事件可以基于 Grid。
英文:
A solution to get the result you want is to wrap your StackPanel in a container (e.g Grid) which will be stretched:
<Window x:Class="panels.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:panels"
mc:Ignorable="d"
Title="MainWindow" Height="600" Width="1000" MouseDown="Window_MouseDown" >
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Grid Grid.Column="1" Grid.Row="0" Background="#FF0019E8">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<Image Source="/icon.png" Height="200" HorizontalAlignment="Center"/>
</StackPanel>
</Grid>
</Grid>
</Window>
The background and the mouse events you want to handle can be based on the Grid.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论