开发者

WPF Alignment not working

开发者 https://www.devze.com 2023-02-21 16:39 出处:网络
I use the following code in my WPF app, for a groupbox: <GroupBox Style=\"{StaticResource groupBoxStyle}\" Header=\"RB\" Margin=\"0,6,268,249\" Name=\"groupBoxRB\" HorizontalAlignment=\"Right\" Ve

I use the following code in my WPF app, for a groupbox:

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

This control is within a grid, that is defined like this:

<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">

And that tabcontrol is within the main grid:

<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>

I don't understand why the groupbox i mentioned won't stretch on its vertical axis... any idea?

Thanks.

PS: the staticresources don't define heights/widths/alignments


My problem came from the designers (vs2010's or blend's) that by default put margins if you place controls manually in them... setting the Margin to 0 solved the problem:

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


I think you're missing something in your styles or something. I just made the following from your code and stretching works fine in standalone app.

    <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>
0

精彩评论

暂无评论...
验证码 换一张
取 消