WPF用户控件不随主窗口大小调整

9

我正在尝试制作一个WPF用户控件,其中包括两个分组框和每个分组框中的两个ListView。以下是用户控件的XAML代码:


    <UserControl x:Class="TestGroupControl.TestGroupControl"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Height="500" Width="700" MinWidth="300" MinHeight="200">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="225*" />
                <RowDefinition Height="225*" />
            </Grid.RowDefinitions>
            <GroupBox Grid.Row="0" Header="Test Data" Margin="2,2,2,2" Name="testDataGroupBox">
                <Grid>
                    <ListView Margin="2,2,2,2" Name="testDataListView" ></ListView>
                </Grid>
            </GroupBox>
            <GroupBox Grid.Row="1" Header="Test Parameters" Margin="2,2,2,2" Name="testParametersGroupBox">
                <Grid>
                    <ListView Margin="2,2,2,2" Name="testParametersListView" ></ListView>
                </Grid>
            </GroupBox>
        </Grid>
    </UserControl>

我面临的问题是,当我尝试调整包含用户控件的主窗口大小时,用户控件不会跟随父窗口一起调整大小。欢迎提出任何想法,谢谢。
2个回答

25

你在UserControl中指定了HeightWidth属性的固定值。请删除这些属性,并将控件实例的HorizontalAlignmentVerticalAlignment属性设置为Stretch。这样就可以解决问题了。


0

如果您在用户控件上使用ViewBox和Canvas,将会修复所有主窗口的大小。

<Viewbox xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" Stretch="Uniform">

  <canvas>

--- all your controls -- 

     </Canvas>
        </Viewbox>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接