如何创建具有水平方向、居中对齐和拉伸的 StackPanel?

huangapple go评论61阅读模式
英文:

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:

&lt;Window x:Class=&quot;panels.MainWindow&quot;
        xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
        xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;
        xmlns:d=&quot;http://schemas.microsoft.com/expression/blend/2008&quot;
        xmlns:mc=&quot;http://schemas.openxmlformats.org/markup-compatibility/2006&quot;
        xmlns:local=&quot;clr-namespace:panels&quot;
        mc:Ignorable=&quot;d&quot;
        Title=&quot;MainWindow&quot; Height=&quot;600&quot; Width=&quot;1000&quot; MouseDown=&quot;Window_MouseDown&quot; &gt;
    &lt;Grid&gt;
        &lt;Grid.ColumnDefinitions&gt;
            &lt;ColumnDefinition Width=&quot;*&quot;/&gt;
        &lt;/Grid.ColumnDefinitions&gt;
        &lt;Grid.RowDefinitions&gt;
            &lt;RowDefinition&gt;&lt;/RowDefinition&gt;
        &lt;/Grid.RowDefinitions&gt;
        &lt;StackPanel Grid.Column=&quot;1&quot; Grid.Row=&quot;0&quot; Orientation=&quot;Horizontal&quot; Background=&quot;#FF0019E8&quot;&gt;
            &lt;Image Source=&quot;/icon.png&quot; Height=&quot;200&quot; HorizontalAlignment=&quot;Center&quot;/&gt;
        &lt;/StackPanel&gt;
    &lt;/Grid&gt;
&lt;/Window&gt;

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:

&lt;Window x:Class=&quot;panels.MainWindow&quot;
    xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
    xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;
    xmlns:d=&quot;http://schemas.microsoft.com/expression/blend/2008&quot;
    xmlns:mc=&quot;http://schemas.openxmlformats.org/markup-compatibility/2006&quot;
    xmlns:local=&quot;clr-namespace:panels&quot;
    mc:Ignorable=&quot;d&quot;
    Title=&quot;MainWindow&quot; Height=&quot;600&quot; Width=&quot;1000&quot; MouseDown=&quot;Window_MouseDown&quot; &gt;
&lt;Grid&gt;
    &lt;Grid.ColumnDefinitions&gt;
        &lt;ColumnDefinition Width=&quot;*&quot;/&gt;
    &lt;/Grid.ColumnDefinitions&gt;
    &lt;Grid.RowDefinitions&gt;
        &lt;RowDefinition&gt;&lt;/RowDefinition&gt;
    &lt;/Grid.RowDefinitions&gt;
    &lt;Grid Grid.Column=&quot;1&quot; Grid.Row=&quot;0&quot; Background=&quot;#FF0019E8&quot;&gt;
        &lt;StackPanel Orientation=&quot;Horizontal&quot; HorizontalAlignment=&quot;Center&quot;&gt;
            &lt;Image Source=&quot;/icon.png&quot; Height=&quot;200&quot; HorizontalAlignment=&quot;Center&quot;/&gt;
        &lt;/StackPanel&gt;
    &lt;/Grid&gt;
&lt;/Grid&gt;

</Window>

The background and the mouse events you want to handle can be based on the Grid.

huangapple
  • 本文由 发表于 2023年7月13日 21:48:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76680128.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定