wpf Xaml设计,调整大小的基本问题

q35jwt9p  于 2023-05-08  发布在  其他
关注(0)|答案(1)|浏览(138)

我是XAML设计的新手,上次我设计用户控件时使用了Windows窗体:-(我习惯于锚控件和停止,当我调整窗体大小时,控件也会相应调整大小。我尝试创建的是这样的x1c 0d1x

  • 第0列和第2列固定在最大标签大小上,第1列和第3列在窗口大小调整时扩展。
  • 文本框2和7在窗口调整大小时展开
  • 文本框3在窗口调整大小时扩展,但它到达右边界(如文本框7)
  • 组合框的大小根据其项目大小而固定
  • RichTextBox靠近标签4,在右边框和下边框上展开,以占据所有可用空间

我试过网格,堆栈面板,但我所有的尝试失败了一些特定的。

编辑

我遵循BionicCode的建议,创建了一个包含网格的用户控件,并将其放入TabControl中,结果是1 experiment
当我调整它的大小时,Texbox 2,3,7和RichTextBox在水平方向上正确调整大小,但还有2个问题:
1.当我调整到一个低宽度的列0和2的大小调整太多,文本被剪切
1.富文本框未垂直展开,因此其下边框和选项卡底部之间有空白
这是我创建的代码

<UserControl x:Class="MyControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:Gui"
         mc:Ignorable="d">

<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Dictionaries\Styles.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</UserControl.Resources>

<Grid Height="auto" Width="auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="10,10,10,10">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="36*"/>
        <ColumnDefinition Width="163*" />
        <ColumnDefinition Width="34*"/>
        <ColumnDefinition Width="165*" />
    </Grid.ColumnDefinitions>

    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />
        <RowDefinition />
        <RowDefinition />
        <RowDefinition Height="auto"/>
    </Grid.RowDefinitions>

    <Label Name="labelCol0"      Grid.Row="0" Grid.Column="0" FontSize="11" FontFamily="Microsoft Sans Serif" TabIndex="1" Margin="1,5,10,5" HorizontalAlignment="Left" >Column 0</Label>
    <Label Name="labelCol1"      Grid.Row="0" Grid.Column="1" FontSize="11" FontFamily="Microsoft Sans Serif" TabIndex="1" Margin="1,5,10,5">Column 1</Label>
    <Label Name="labelCol2"      Grid.Row="0" Grid.Column="2" FontSize="11" FontFamily="Microsoft Sans Serif" TabIndex="1" Margin="1,5,10,5">Column 2</Label>
    <Label Name="labelCol3"      Grid.Row="0" Grid.Column="3" FontSize="11" FontFamily="Microsoft Sans Serif" TabIndex="1" Margin="1,5,10,5">Column 3</Label>

    <Label Name="label1"        Grid.Row="1" Grid.Column="0" FontSize="11" FontFamily="Microsoft Sans Serif" TabIndex="1" Margin="1,5,10,5"  HorizontalAlignment="Left">label 1</Label>
    <ComboBox Name="_cbo1"    Grid.Row="1" Grid.Column="1" Width="93" HorizontalAlignment="Left" TabIndex="0" Margin="0,5,0,5" />
    <Label Name="label6"     Grid.Row="1" Grid.Column="2" FontSize="11" FontFamily="Microsoft Sans Serif" TabIndex="4" Margin="1,5,0,5"  HorizontalAlignment="Left">label 6</Label>
    <ComboBox Name="_cbo2"  Grid.Row="1" Grid.Column="3" Width="76" TabIndex="3" HorizontalAlignment="Left" Margin="0,5,0,5" />

    <Label Name="label2"    Grid.Column="0" Grid.Row="2" FontSize="11" FontFamily="Microsoft Sans Serif" TabIndex="8" HorizontalAlignment="Left" Margin="1,5,0,5" Width="42" >label 2</Label>
    <TextBox Name="_txt2" Grid.Column="1" Grid.Row="2" TabIndex="6" TextWrapping="Wrap" Margin="0,6,10,6" Text="TextBox 2"/>
    <Label Name="label7"       Grid.Column="2" Grid.Row="2" Width="47" FontSize="11" FontFamily="Microsoft Sans Serif" TabIndex="9" HorizontalAlignment="Left" Margin="0,5,0,5" >label 7</Label>
    <TextBox Name="_txt7"    Grid.Column="3" Grid.Row="2" Width="auto" TabIndex="7" TextWrapping="Wrap" Margin="1,5,10,5" Text="TextBox 7" />

    <Label Name="label3"        Grid.Column="0" Grid.Row="3" Width="57" FontSize="11" FontFamily="Microsoft Sans Serif" TabIndex="13" HorizontalAlignment="Left" Margin="1,5,0,5" >label 3</Label>
    <TextBox Name="_txt3" Grid.Column="1" Grid.Row="3" Grid.ColumnSpan="3" TabIndex="12" TextWrapping="Wrap" Margin="0,5,10,5" Text="TextBox 3" />

    <Label Name="label4"        Grid.Column="0" Grid.Row="4" Width="42" FontSize="11" FontFamily="Microsoft Sans Serif" TabIndex="24" HorizontalAlignment="Left" Margin="1,72,0,73" >label 4</Label>
    <RichTextBox Name="richTextBox4" Grid.Column="1" Grid.Row="4" Grid.ColumnSpan="3" Width="auto" TabIndex="25" Margin="1,5,10,10" >
        <FlowDocument>
            <Paragraph>
                <Run></Run>
            </Paragraph>
        </FlowDocument>
    </RichTextBox>

</Grid>
f87krz0w

f87krz0w1#

我找到解决办法了
我把一切都简化了。
我的MainWindow.xaml是

<Window x:Class="Client.MainWindow2"
    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:Client"
    mc:Ignorable="d"
    Title="MainWindow" MinHeight="600" MinWidth="700" SizeChanged="Window_SizeChanged">

<Grid Name="mainGrid" >
    <Grid.RowDefinitions>
        <RowDefinition Height="auto"/>
        <RowDefinition Height="*" />
        <RowDefinition Height="auto"/>
    </Grid.RowDefinitions>

    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="auto" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>

    <Menu x:Name="_mainMenuStrip" IsMainMenu="True"         Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2">
        <MenuItem Header="_File" Name="fileMenuItem" Click="newMenuItem_Click"/>
    </Menu>
    <Image Source="Resources/NewMaterial.png" Width="79"    Grid.Row="1" Grid.Column="0" />

    <TabControl Name="mainTabControl"                       Grid.Row="1" Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>

    <StatusBar x:Name="statusStrip1"                        Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2" >
        <Label x:Name="lblSize" Content="Label"/>
    </StatusBar>
</Grid>

和我的控件是

<UserControl x:Class="Gui.PersonView3"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:Gui"
         mc:Ignorable="d">

<Grid Name="mainGrid" Margin="10,10,10,10" ShowGridLines="True" >
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="auto" />
        <ColumnDefinition Width="163*" />
        <ColumnDefinition Width="auto"/>
        <ColumnDefinition Width="165*" />
    </Grid.ColumnDefinitions>

    <Grid.RowDefinitions>
        <RowDefinition Height="auto"/>
        <RowDefinition Height="auto" />
        <RowDefinition Height="auto" />
        <RowDefinition Height="auto" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>

    <Label Name="labelCol0"     Grid.Row="0" Grid.Column="0" FontSize="11" FontFamily="Microsoft Sans Serif" TabIndex="1" Margin="1,5,10,5" HorizontalAlignment="Left" >Column 0</Label>
    <Label Name="labelCol1"     Grid.Row="0" Grid.Column="1" FontSize="11" FontFamily="Microsoft Sans Serif" TabIndex="1" Margin="1,5,10,5">Column 1</Label>
    <Label Name="labelCol2"     Grid.Row="0" Grid.Column="2" FontSize="11" FontFamily="Microsoft Sans Serif" TabIndex="1" Margin="1,5,10,5">Column 2</Label>
    <Label Name="labelCol3"     Grid.Row="0" Grid.Column="3" FontSize="11" FontFamily="Microsoft Sans Serif" TabIndex="1" Margin="1,5,10,5">Column 3</Label>

    <Label Name="label1"        Grid.Row="1" Grid.Column="0" FontSize="11" FontFamily="Microsoft Sans Serif" TabIndex="1" Margin="1,5,10,5"  HorizontalAlignment="Left">label 1</Label>
    <ComboBox Name="_cbo1"      Grid.Row="1" Grid.Column="1" Width="93" HorizontalAlignment="Left" TabIndex="0" Margin="0,5,0,5" />
    <Label Name="label6"        Grid.Row="1" Grid.Column="2" FontSize="11" FontFamily="Microsoft Sans Serif" TabIndex="4" Margin="1,5,0,5"  HorizontalAlignment="Left">label 6</Label>
    <ComboBox Name="_cbo2"      Grid.Row="1" Grid.Column="3" Width="76" TabIndex="3" HorizontalAlignment="Left" Margin="0,5,0,5" />

    <Label Name="label2"        Grid.Column="0" Grid.Row="2" FontSize="11" FontFamily="Microsoft Sans Serif" TabIndex="8" HorizontalAlignment="Left" Margin="1,5,0,5" Width="42" >label 2</Label>
    <TextBox Name="_txt2"       Grid.Column="1" Grid.Row="2" TabIndex="6" TextWrapping="Wrap" Margin="0,6,10,6" Text="TextBox 2"/>
    <Label Name="label7"        Grid.Column="2" Grid.Row="2" Width="47" FontSize="11" FontFamily="Microsoft Sans Serif" TabIndex="9" HorizontalAlignment="Left" Margin="0,5,0,5" >label 7</Label>
    <TextBox Name="_txt7"       Grid.Column="3" Grid.Row="2" Width="auto" TabIndex="7" TextWrapping="Wrap" Margin="1,5,10,5" Text="TextBox 7" />

    <Label Name="label3"        Grid.Column="0" Grid.Row="3" Width="57" FontSize="11" FontFamily="Microsoft Sans Serif" TabIndex="13" HorizontalAlignment="Left" Margin="1,5,0,5" >label 3</Label>
    <TextBox Name="_txt3"       Grid.Column="1" Grid.Row="3" Grid.ColumnSpan="3" TabIndex="12" TextWrapping="Wrap" Margin="0,5,10,5" Text="TextBox 3" />

    <Label Name="label4"        Grid.Column="0" Grid.Row="4" Width="42" FontSize="11" FontFamily="Microsoft Sans Serif" TabIndex="24" HorizontalAlignment="Left" Margin="1,5,0,73" >label 4</Label>
    <RichTextBox Name="richTextBox4" Grid.Column="1" Grid.Row="4" Grid.ColumnSpan="3" Width="auto" TabIndex="25" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="1,5,10,10" >
    </RichTextBox>

</Grid>

,问题与控件无关,而是与“控件创建”有关。
当我创建只包含用户控件的选项卡时,我设置了属性
tab.HorizontalContentAlignment = HorizontalAlignment.拉伸; tab.VerticalContentAlignment = VerticalAlignment.Top;
将第二个属性更改为
tab.VerticalContentAlignment = VerticalAlignment.Stretch;
一切正常
谢谢

相关问题