Dot-Net

WPF 使用者控制項不隨主視窗調整大小

  • May 15, 2019

我正在嘗試製作一個 WPF 使用者控制項,其中包含兩個組框和ListView每個組框中的兩個 s。這是使用者控制項的 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>

我面臨的問題是,當我嘗試調整持有使用者控制項的主視窗的大小時,使用者控制項不會跟隨父視窗並調整大小。

任何想法表示讚賞。謝謝你。

您在 UserControl 中將HeightWidth屬性指定為固定值。刪除這些屬性,並將主視窗中控制項實例的HorizontalAlignment和屬性設置為。那應該照顧它。VerticalAlignment``Stretch

如果您在使用者控制項上使用 ViewBox 和 Canvas 將修復所有主視窗大小

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

 <canvas>

--- all your controls -- 

    </Canvas>
       </Viewbox>

引用自:https://stackoverflow.com/questions/2976393