如何让viewbox保持其大小?

问题描述 投票:0回答:1

在 wpf 应用程序中,我有一个带有

Grid
“ContentGrid”的用户控件,它可以变得比用户控件更大,然后 ContentGrid 的所有父控件的大小都调整得太大。

我不明白为什么

Viewbox
不能完成其工作并保持内部所有内容的大小调整,以便人们可以看到所有内容,而无需调整外部控件的大小。

特别是,这个用户控件“myUC”被放置在另一个网格内,在窗口内;当ContentGrid变得太大时,用户控件也会变得太大,并且最父网格会溢出超出Window的限制。

如何确保视图框的内容不会调整其父级的大小? 或者如何确保大多数父网格不会溢出窗口?

<UserControl x:Class="myUC"
             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:DesignHeight="450" d:DesignWidth="800">
    <Grid>
        <Viewbox>
            <Grid Name="ContentGrid">

简化窗口:

<Window>
    <Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="5"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    
    <myUC Grid.Row="0", Grid.RowSpan="1"/>
c# wpf xaml
1个回答
0
投票

方法是将视图框的

MaxHeight
绑定到用户控件的
ActualHeight
,因此它永远不会比用户控件大:

<UserControl x:Class="myUC">
    <Grid>
        <Viewbox MaxHeight="{Binding ActualHeight,
         RelativeSource={RelativeSource AncestorType=local:myUC}}">
© www.soinside.com 2019 - 2024. All rights reserved.