wpf 调整父窗口大小时自动调整用户控件的大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20032574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 10:03:57  来源:igfitidea点击:

Automatic resizing of user controls when parent window is resized

c#wpfwindow-resize

提问by Boardy

I am working on a C# WPF project and I am making use of user controls but I am having a problem with getting the resize to work when the parent window is resized.

我正在处理一个 C# WPF 项目,我正在使用用户控件,但是在调整父窗口大小时,我在调整大小时遇到​​问题。

I have a Window called MainWindow which houses a canvas which will host the user controls. When Main Window is resized the user controls should also resize. This is more a less working except it doesn't seem to fill up the who screen. Below is the code and screenshots to explain what I mean.

我有一个名为 MainWindow 的窗口,它包含一个用于承载用户控件的画布。调整主窗口大小时,用户控件也应调整大小。除了它似乎没有填满 who 屏幕之外,这更有效。下面是解释我的意思的代码和屏幕截图。

Main Window

主窗口

Below is the code for the Main Window, this window will host the user controls

下面是主窗口的代码,这个窗口将承载用户控件

<Window x:Class="ReportReader.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="513" Width="925" WindowStartupLocation="CenterScreen">
    <Grid>
        <Menu Height="23" Name="menu1" VerticalAlignment="Top">
            <MenuItem Name="mnuFile" Header="File">
                <MenuItem Name="mnuOpen" Click="mnuOpen_Click" Header="Open" />
                <MenuItem Name="mnuClose" Click="mnuClose_Click" Header="Close Report" />
                <MenuItem Name="mnuRecentFile" Header="Recent Files" />
                <Separator />
                <MenuItem Name="mnuExit" Click="mnuExit_Click" Header="Exit" InputGestureText="Alt+F4" />
            </MenuItem>
        </Menu>
        <StatusBar Name="statusBar1" Height="28" VerticalAlignment="Bottom" />
        <Label Content="No Report Loaded" Name="lblStatus" Height="28" VerticalAlignment="Bottom" Margin="0,0,39,0" />
        <Grid Margin="12,29,12,34" Name="MainWindowHostCanvas" Background="Blue" />
    </Grid>
</Window>

The canvas is coloured blue so it shows my problem in the screenshots.

画布是蓝色的,所以它在屏幕截图中显示了我的问题。

Below is the code for one of the user controls

以下是其中一个用户控件的代码

<UserControl x:Class="ReportReader.UserControls.ReportViewer"
             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" mc:Ignorable="d" d:DesignWidth="879">
    <Grid Height="415">
        <Label Content="Report for..." Margin="12,12,12,0" Name="lblReportDateTitle" FontSize="26" FontWeight="Bold" HorizontalContentAlignment="Center" Height="44" VerticalAlignment="Top" />
        <ComboBox Height="23" HorizontalAlignment="Left" Margin="12,62,0,0" Name="cboRegisteredApps" VerticalAlignment="Top" Width="202" SelectionChanged="cboRegisteredApps_SelectionChanged">
            <ComboBoxItem Content="Select App" IsSelected="True" />
        </ComboBox>
        <DataGrid AutoGenerateColumns="False" Margin="14,203,12,12" Name="dataExceptionGroups" />
    </Grid>
</UserControl>

Below is a screenshot of the User Control hosted by the Main Window from when the program first loaded up.

下面是程序第一次加载时由主窗口托管的用户控件的屏幕截图。

Screenshot from when application first loadedAs you can see the blue canvas fills the whole window up to the status bar, the data grid, is at the bottom of the window, just above the status bar, and the report title is at the top of the window just above the menu bar.

应用程序首次加载时的屏幕截图如您所见,蓝色画布填满了整个窗口直到状态栏,数据网格位于窗口底部,状态栏正上方,报表标题位于窗口顶部菜单正上方酒吧。

Below is the screen shot now that the window has been resized

下面是窗口已调整大小后的屏幕截图

Screenshot of window after it has been resizedAs you can see from this screenshot, the window has been resized and the blue canvas has resized to fill up the extra space, however, the datagrid is no longer at the bottom of the window, just above the status bar and instead floating around the middle. Also the title at the top is no longer just under the menu bar, and is also in the middle.

调整大小后的窗口屏幕截图正如您在此屏幕截图中所见,窗口已调整大小且蓝色画布已调整大小以填充额外空间,但是,数据网格不再位于窗口底部,而是位于状态栏上方,而是漂浮在中间。此外,顶部的标题不再只是在菜单栏下方,也位于中间。

I'm not sure what I can do to resolve this.

我不确定我能做些什么来解决这个问题。

Thanks for any help you can provide.

感谢您的任何帮助,您可以提供。

采纳答案by dkozl

Your Gridwithin UserControlhas fixed Heightset to 415 which is why it does not stretch:

您的Grid内部UserControl已固定Height设置为 415,这就是它不拉伸的原因:

<UserControl>
    <Grid Height="415">

If you're using designer to create your interface then it has tendency to set these properties to fixed values.

如果您使用设计器来创建您的界面,那么它倾向于将这些属性设置为固定值。

回答by Kumareshan

If Height="*" is set then all the rows would share the available space equally. If you want to set in % then you can set some thing like this

如果设置了 Height="*",则所有行将平均共享可用空间。如果你想设置 % 那么你可以设置一些这样的东西

<Grid.RowDefenitions>
      <RowDefenition Height="10*"/>
      <RowDefenition Height="20*"/>
      <RowDefenition Height="60*"/>
      <RowDefenition Height="10*"/>   
 </Grid.RowDefenitions>

now when ever the window is re-sized always same percentage will be shared for all the rows

现在,当窗口重新调整大小时,所有行都会共享相同的百分比

It is not necessary the height value in all the rows should sum up to 100. For example if you have three rows then you can set as below as well

不需要所有行的高度值总和为 100。例如,如果您有三行,那么您也可以设置如下

<Grid.RowDefenitions>
      <RowDefenition Height="20*"/>
      <RowDefenition Height="30*"/>
      <RowDefenition Height="10*"/>  
 </Grid.RowDefenitions>

then the total window size will be divided among the all the three rows as follows

那么总的窗口大小将在所有三行之间划分如下

for 1st row height will be WindowSize*20/(20+30+10)
for 2nd row height will be WindowSize*30/(20+30+10)
for 3rd row height will be WindowSize*10/(20+30+10)

第一行高度为 WindowSize*20/(20+30+10)
第二行高度为 WindowSize*30/(20+30+10)
第三行高度为 WindowSize*10/(20+30+10) )

All the Row sizes will re-size accordingly when ever window size is re-sized

当窗口大小重新调整大小时,所有行大小都会相应地重新调整大小

回答by eran otzap

You are using Static Sizing with margin's and height's with hard coded values.

您正在使用带有硬编码值的边距和高度的静态大小调整。

1) Remove all the hard coded values for margin's height and width.

1) 删除边距高度和宽度的所有硬编码值。

2) Split your grid using Row/Column Definitions with relative height using * , FYI you can set MinHeight on RowDefenition .

2)使用具有相对高度的行/列定义拆分网格 * ,仅供参考,您可以在 RowDefenition 上设置 MinHeight 。

 <Grid>
     <Grid.RowDefenitions>
          <RowDefenition Height="*"/>
          <RowDefenition Height="*"/>
          <RowDefenition Height="*"/>
          <RowDefenition Height="*"/>   
     </Grid.RowDefenitions>

     <Menu>
        .....
    </Menu>
    <StatusBar Grid.Row="1"/>
    <Label Content="No Report Loaded"  Grid.Row="2"  />
    <Grid Grid.Row="3" />
</Grid>

Do the same for the UserControl.

对 UserControl 执行相同的操作。