【问题标题】:WPF Alignment not workingWPF对齐不起作用
【发布时间】:2011-07-28 21:17:29
【问题描述】:

我在我的 WPF 应用程序中使用以下代码作为组框:

<GroupBox Style="{StaticResource groupBoxStyle}" Header="RB" Margin="0,6,268,249" Name="groupBoxRB" HorizontalAlignment="Right" VerticalAlignment="Stretch" Width="276">

此控件位于网格内,其定义如下:

<TabControl Grid.Row="1" Margin="4,12,0,12" Name="tabControl1" Background="Transparent">
    <TabItem Style="{StaticResource tabItemMainStyle}" Header="Main" Name="tabItemMain" >
        <Grid Background="Transparent" MinHeight="926" MinWidth="1218">

并且该选项卡控件位于主网格内:

<Window x:Class="SRM.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
        xmlns:local="clr-namespace:SRM" ResizeMode="CanResize"
        Title="SRM"
        Width="991" Icon="Resources\Icons\SRM.png"
        WindowStartupLocation="CenterScreen" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" d:DesignHeight="1024" Height="774" Visibility="Visible" Foreground="#00000000" Margin="0">
    <Grid Style="{StaticResource styleBackground}">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="2.5" />
        </Grid.ColumnDefinitions>

我不明白为什么我提到的 groupbox 不会在其垂直轴上伸展...有什么想法吗?

谢谢。

PS:静态资源没有定义高度/宽度/对齐方式

【问题讨论】:

  • 您的groupBoxStyle 是否定义了自定义ControlTemplate?如果有,我们能看到吗?
  • 另外,如果去掉内部 Grid 中的 MinHeight 和 GroupBox 周围的大 Margin 是否还有问题?
  • groupBoxStyle 仅定义设置器(颜色/画笔/不透明度)。边距使组框不会相互重叠,但如果有更好的用途,它可以被替换。 Minheight 可以毫无问题地删除。
  • 组框不相互重叠是什么意思?如果组框位于网格中,您可以使用列和行来控制布局,这样它们就不会相互重叠,我相信这是造成这个问题的大底边距。
  • @FatalBaboon,您是否尝试过使用Snoop 查看哪个项目没有拉伸(组框、网格或选项卡项目)?

标签: wpf alignment


【解决方案1】:

我的问题来自设计师(vs2010 或 blend 的),如果您在其中手动放置控件,默认情况下会放置边距...将边距设置为 0 解决了问题:

<GroupBox Style="{StaticResource groupBoxStyle}" Header="RB" Margin="0" Name="groupBoxRB" HorizontalAlignment="Right" VerticalAlignment="Stretch" Width="276">

【讨论】:

    【解决方案2】:

    我认为你在你的风格或其他东西上缺少了一些东西。我刚刚从您的代码中制作了以下内容,并且在独立应用程序中拉伸工作正常。

        <Window x:Class="WpfApplication1.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="2.5" />
            </Grid.ColumnDefinitions>
            <TabControl Grid.Row="1" Margin="4,12,0,12" Name="tabControl1" Background="Transparent">
                <TabItem Header="Main" Name="tabItemMain" >
                    <Grid Background="Transparent" MinHeight="200" MinWidth="200">
                        <GroupBox Header="RB" Name="groupBoxRB" HorizontalAlignment="Right" VerticalAlignment="Stretch" Width="276">
                            <Rectangle Fill="Orange" />
                        </GroupBox>
                    </Grid>
                </TabItem>
            </TabControl>
        </Grid>
    </Window>
    

    【讨论】:

    • 我想我记得前段时间遇到过同样的问题。从我的记忆中,我注意到每当我在新计算机上从颠覆中获取我的解决方案时,stetching 行为和类似行为都会被搞砸。铃响了吗?
    • 很抱歉我遇到过这种情况。可能是来自 TabControl 或其他东西的默认模板。要尝试的一件事是为每个连续的 UI 元素设置填充颜色,这样您就可以找出第一个没有正确拉伸的控件 - 这将通过非常清楚地着色正在发生的事情来帮助找出哪个元素是罪魁祸首。
    猜你喜欢
    • 1970-01-01
    • 2014-10-10
    • 2015-02-01
    • 2016-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多