首页 > 解决方案 > 初始化后调整托管在其父窗口中的 UserControl 的大小

问题描述

我正在使用一个窗口来承载几个不同的用户控件作为一个对话框。窗口基本上是这样的:

 <Window x:Class="GenericWindow"
        x:Name="BaseDialog"
        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"
        WindowStartupLocation="CenterScreen"
        d:DataContext="{d:DesignInstance Type=wpfDialogs:DialogViewModel, 
IsDesignTimeCreatable=True}"
        Title="{Binding Title}"
        SizeToContent="WidthAndHeight"
        MinWidth="400" MinHeight="400"
        WindowStyle="ToolWindow"
        ResizeMode="CanResizeWithGrip"
        Loaded="Window_Loaded">

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

    <ContentPresenter Grid.Row="0" Margin="10 10 10 0" Content="{Binding}"/>

 </Window>

我的用户控件看起来像这样:

<UserControl x:Class="PickControl"
    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:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm"
    xmlns:dxprg="http://schemas.devexpress.com/winfx/2008/xaml/propertygrid"        
    xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"        
    mc:Ignorable="d"
    x:Name="mainWindow">

  <Grid>
    <Grid.RowDefinitions>
      <RowDefinition Height ="600" MinHeight="200"/>
      <RowDefinition Height ="15"/>
      <RowDefinition Height ="400" MinHeight="100"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
  <ColumnDefinition Width ="700"/>
</Grid.ColumnDefinitions>


<-- content goes here -->
</UserControl>

启动时,我的对话框具有托管用户控件中网格的大小。没关系。但是在调整外部窗口的大小时,用户控件内的网格​​将保持相同的静态设置大小。我希望外部窗口在初始化时具有内容的大小,然后使用外部窗口调整内容的大小。这可能吗?

谢谢

标签: c#wpf

解决方案


  1. 如果您不总是希望它具有固定大小,请从定义中删除RowDefinitionsand 。ColumnDefinitionsUserControl

  2. 在窗口中设置它的初始大小,然后处理SizeChanged两次事件:

     private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
     {
         SizeChanged -= Window_SizeChanged;
         SizeChanged += RemoveInitialSizeConstraints;
     }
    
     private void RemoveInitialSizeConstraints(object sender, SizeChangedEventArgs e)
     {
         cc.Height = double.NaN;
         cc.Width = double.NaN;
         SizeChanged -= RemoveInitialSizeConstraints;
     }
    

XAML:

<Window x:Class="GenericWindow"
        x:Name="BaseDialog"
        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"
        WindowStartupLocation="CenterScreen"
        d:DataContext="{d:DesignInstance Type=wpfDialogs:DialogViewModel, 
IsDesignTimeCreatable=True}"
        Title="{Binding Title}"
        SizeToContent="WidthAndHeight"
        MinWidth="400" MinHeight="400"
        WindowStyle="ToolWindow"
        ResizeMode="CanResizeWithGrip"
        Loaded="Window_Loaded"
        SizeChanged="Window_SizeChanged">

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

    <ContentPresenter x:Name="cc" Grid.Row="0" Margin="10 10 10 0" Content="{Binding}"
                      Height ="600" MinHeight="200" Width="700"/>

</Window>

推荐阅读