gpt4 book ai didi

wpf - 并排放置堆叠面板

转载 作者:行者123 更新时间:2023-12-04 22:56:04 29 4
gpt4 key购买 nike

我需要将控件分组并将它们并排放置。我想出了这个代码来使用多个 StackPanel要做到这一点。

<Window x:Class="xamlTests.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="310" Width="525">
<Grid>

<StackPanel x:Name="_ribbonRadioButtonPanel" Orientation="Vertical">
<CheckBox Content="Signed" Height="16" Name="Signed" Checked="Signed_Checked" Margin="10,5"/>
<StackPanel x:Name="_wordLength" Orientation="Horizontal">
<TextBox Height="18" Name="textBoxWordLength" Width="30" Margin="10,5"/>
<TextBlock Height="20" Name="textBlockWordLength" Text="Word Length" Width="120"/>
</StackPanel>
<StackPanel x:Name="_integerWordLength" Orientation="Horizontal">
<TextBox Height="18" Name="textBoxIntegerWordLength" Width="30" Margin="10,5"/>
<TextBlock Height="20" Name="textBlockIntegerWordLength" Text="Integer Word Length" Width="120"/>
</StackPanel>
</StackPanel>

<StackPanel x:Name="_ribbonRadioButtonPanel2">
<StackPanel x:Name="_max" Orientation="Horizontal">
<TextBox Height="18" Name="maxTextBox" Width="100" Margin="10,5"/>
<TextBlock Height="20" Name="maxTextBlock" Text="Max" Width="120"/>
</StackPanel>
<StackPanel x:Name="_min" Orientation="Horizontal">
<TextBox Height="18" Name="minTextBox" Width="100" Margin="10,5"/>
<TextBlock Height="20" Name="minTextBlock" Text="Min" Width="120"/>
</StackPanel>
<StackPanel x:Name="_delta" Orientation="Horizontal">
<TextBox Height="18" Name="deltaTextBox" Width="100" Margin="10,5"/>
<TextBlock Height="20" Name="delatTextBlock" Text="Delta" Width="120"/>
</StackPanel>
</StackPanel>
</Grid>
</Window>

然而,我得到了 StackPanels 的过度应用。 XAML 有什么问题?哪些布局面板用于对齐多个组件?

enter image description here

最佳答案

您可以执行以下操作...

        <Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<StackPanel x:Name="_ribbonRadioButtonPanel" Orientation="Vertical" Grid.Column="0">
<CheckBox Content="Signed" Height="16" Name="Signed" Checked="Signed_Checked" Margin="10,5"/>
<StackPanel x:Name="_wordLength" Orientation="Horizontal">
<TextBox Height="18" Name="textBoxWordLength" Width="30" Margin="10,5"/>
<TextBlock Height="20" Name="textBlockWordLength" Text="Word Length" Width="120"/>
</StackPanel>
<StackPanel x:Name="_integerWordLength" Orientation="Horizontal">
<TextBox Height="18" Name="textBoxIntegerWordLength" Width="30" Margin="10,5"/>
<TextBlock Height="20" Name="textBlockIntegerWordLength" Text="Integer Word Length" Width="120"/>
</StackPanel>
</StackPanel>
<StackPanel x:Name="_ribbonRadioButtonPanel2" Grid.Column="1">
<StackPanel x:Name="_max" Orientation="Horizontal">
<TextBox Height="18" Name="maxTextBox" Width="100" Margin="10,5"/>
<TextBlock Height="20" Name="maxTextBlock" Text="Max" Width="120"/>
</StackPanel>
<StackPanel x:Name="_min" Orientation="Horizontal">
<TextBox Height="18" Name="minTextBox" Width="100" Margin="10,5"/>
<TextBlock Height="20" Name="minTextBlock" Text="Min" Width="120"/>
</StackPanel>
<StackPanel x:Name="_delta" Orientation="Horizontal">
<TextBox Height="18" Name="deltaTextBox" Width="100" Margin="10,5"/>
<TextBlock Height="20" Name="delatTextBlock" Text="Delta" Width="120"/>
</StackPanel>
</StackPanel>
</Grid>

这会将控件放入单独的列中,以便它们不会重叠。另一种方法是将堆栈面板放入其方向设置为水平的堆栈面板中,如下所示......
        <StackPanel Orientation="Horizontal">
<StackPanel x:Name="_ribbonRadioButtonPanel" Orientation="Vertical" Grid.Column="0">
<CheckBox Content="Signed" Height="16" Name="Signed" Checked="Signed_Checked" Margin="10,5"/>
<StackPanel x:Name="_wordLength" Orientation="Horizontal">
<TextBox Height="18" Name="textBoxWordLength" Width="30" Margin="10,5"/>
<TextBlock Height="20" Name="textBlockWordLength" Text="Word Length" Width="120"/>
</StackPanel>
<StackPanel x:Name="_integerWordLength" Orientation="Horizontal">
<TextBox Height="18" Name="textBoxIntegerWordLength" Width="30" Margin="10,5"/>
<TextBlock Height="20" Name="textBlockIntegerWordLength" Text="Integer Word Length" Width="120"/>
</StackPanel>
</StackPanel>
<StackPanel x:Name="_ribbonRadioButtonPanel2" Grid.Column="1">
<StackPanel x:Name="_max" Orientation="Horizontal">
<TextBox Height="18" Name="maxTextBox" Width="100" Margin="10,5"/>
<TextBlock Height="20" Name="maxTextBlock" Text="Max" Width="120"/>
</StackPanel>
<StackPanel x:Name="_min" Orientation="Horizontal">
<TextBox Height="18" Name="minTextBox" Width="100" Margin="10,5"/>
<TextBlock Height="20" Name="minTextBlock" Text="Min" Width="120"/>
</StackPanel>
<StackPanel x:Name="_delta" Orientation="Horizontal">
<TextBox Height="18" Name="deltaTextBox" Width="100" Margin="10,5"/>
<TextBlock Height="20" Name="delatTextBlock" Text="Delta" Width="120"/>
</StackPanel>
</StackPanel>
</StackPanel>

可能还有许多其他方法可以做到这一点以获得所需的结果。

关于wpf - 并排放置堆叠面板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6696310/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com