在Viewbox's Children上强制大小

本文关键字:Children Viewbox | 更新日期: 2023-09-27 18:06:48

所以我有一个相当有趣的问题。我有一个viewbox,其中有几个元素(用于图像、画布、标签和文本框的自定义用户控件)。我想要的是尝试让所有元素与视图框一起缩放,但我想要标签和文本框有一个"Max Size"。我试过在这些控件上使用最大宽度和高度,但它们似乎忽略了它。如果有人能看看我下面的代码,打我一巴掌,我做错了,将不胜感激。

<Viewbox Name="myViewBox" Stretch="Uniform">
  <!--Grid used to track mouse movements in this element for other reasons -->
  <Grid Name="grdViewboxGrid" MouseMove="trackMouse">
    <Canvas Name="cvsViewboxCanvas" MinWidth="270" MinHeight="270"   
            VerticalAlignment="Top" HorizontalAlignment="Center" 
            Panel.ZIndex="1" Background="Black"
            MouseUp="Canvas_MouseUp"
            MouseMove="Canvas_MouseMove">
      <Grid>
        <!--Would rather not post here for Intellectual Property reasons-->
        <!-- Extension of the image control -->
        <CustomImageUserControl />
        <Grid>
          <Grid Width="{Binding LabelWidthPercentage}"
                MaxWidth="50"
                Height="{Binding LabelHeightPercentage"
                MaxHeight="26"
                SnapsToDevicePixels="True" VerticalAlignment="Top"
                HorizontalAlignment="Left" Margin="5" IsHitTestVisible="False">
            <Label Name="lblViewboxLabel" HorizontalAlignment="Left"
                   Padding="5,5,5,0" Margin="0,5,0,0"
                   Style="{x:Null}"
                   Content="{Binding lblContent}" />
          </Grid>
          <Grid>
            <Grid Width="{Binding TextBoxWidthPercentage}"
                  MaxWidth="156"
                  Height="{Binding TextBoxHeightPercentage}"
                  MaxHeight="45"
                  SnapsToDevicePixels="True" Vertical="Top"
                  HorizontalAlignment="Right" Margin="5" IsHitTestVisible="False">
              <Border Style="{DynamicResource CustomBorder}" />
              <Grid>
                <Textbox Name="txtViewboxTextBox" Text="{Binding txtViewbox}" />
              </Grid>
            </Grid>
          </Grid>
        </Grid>
      </Grid>
    </Canvas>
  </Grid>
</Viewbox>

如果我没有包括需要的东西,请让我知道,我会更新我的问题。任何帮助都将是非常感激的,现在是这个问题的第4天了,很遗憾:

在Viewbox's Children上强制大小

我不知道为什么你需要这么多重叠的网格,但我希望我能回答你的问题:

为了使标签位于文本框的左侧,并为这两个控件分配最大宽度,使用具有两列的Grid,并为每列设置MaxWidth属性。然后,将标签分配给左列(索引为0的列),并将文本框分配给右列(索引为1)。相应的代码片段如下:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition MaxWidth="30"/>
        <ColumnDefinition MaxWidth="156" MinWidth="30"/>
    </Grid.ColumnDefinitions>
    <Label Grid.Column="0" x:Name="lblViewboxLabel" HorizontalAlignment="Left" Foreground="Yellow"
        Padding="5,5,5,0" Margin="0,5,0,0"
        Style="{x:Null}"
        Content="{Binding lblContent}" />
    <TextBox Grid.Column="1" x:Name="txtViewboxTextBox" Text="{Binding txtViewbox}" Background="Orange"/>
</Grid>

我也给右列分配了一个MinWidth;这是必要的,以确保文本框不会完全消失,如果它不包含文本。