Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 视图框上的强制大小';孩子们_C#_Wpf_Viewbox - Fatal编程技术网

C# 视图框上的强制大小';孩子们

C# 视图框上的强制大小';孩子们,c#,wpf,viewbox,C#,Wpf,Viewbox,所以我有一个很有趣的问题。我有一个viewbox,其中包含一些元素(用于图像、画布、标签和文本框的自定义用户控件)。我想要的是尝试让所有元素与viewbox一起缩放,但我希望标签和文本框具有“最大大小”。我曾尝试在这些控件上使用最大宽度和高度,但它们似乎忽略了它。如果有人能看看我下面的代码,并为我做错的事打我一巴掌,我将不胜感激 <Viewbox Name="myViewBox" Stretch="Uniform"> <!--Grid used to track mouse

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

<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天,非常感谢您提供的任何帮助。很遗憾:(

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

要使标签位于文本框的左侧,并为这两个控件中的每一个控件指定最大宽度,请使用具有两列的
网格
,并为每列设置
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
;这是必要的,以确保文本框在不包含文本时不会完全消失。

我对设置最大宽度和最大高度感兴趣。我确实尝试了您在此处发布的内容,但将行定义设置为最大高度50(标签的最大值只需为26,但如果需要,我愿意使用更大的标签)。然而,这导致标签和文本框被切断,而不是强制执行“最大刻度”至于网格,有这么多是因为我试图处理Z顺序和通过容器进行缩放。我现在认为这可能是一个糟糕的举动。