0

So I have a rather interesting question. I have a viewbox that has a few elements in it (a custom user control for an image, a canvas, a label, and a textbox). What I want is to try and have all elements scale with the viewbox, but I want the label and the textbox to have a "Max Size." I have tried using a max width and height on these controls but they seem to ignore it. If someone could take a look at my code below an slap me for what I am doing wrong that would be appreciated.

<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>

If I am not including something that is needed please let me know and I will update my question. Any help would be greatly appreciated this is now day 4 on this issue sadly :-(

Jimmy
  • 941
  • 2
  • 8
  • 27

1 Answers1

0

I am not sure why you need so many overlapping Grids, but I hope that I can answer your question nevertheless:

In order to have the label left of the text box and to assign a maximum width to each of these two controls, use a Grid with two columns and set the MaxWidth property for each column. Then, assign the label to the left column (the one with index 0) and assign the text box to the right column (index 1). The corresponding code fragment looks like this:

<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>

I also have assigned a MinWidth to the right column; this is necessary to make sure that the text box does not completely disappear if it contains no text.

  • I am interested in setting a max width AND a max height. I did try as you have posted here but set a row definition with a max height of 50 (the label only needs to be a max of 26 but I am willing to have a larger label if need be). However this caused the label and textbox to be cut off instead of enforcing a "Max scale." As for the grids, there are this many because I was attempting to handle Z-Order and scaling through containers. I am thinking now that may have been a poor move. – Jimmy Jun 05 '14 at 17:27