views:

352

answers:

1

I have a popup within a user control. The popup uses a TextBox to show a textual preview of data produced by the control.

How do I make the popup size itself to the user control it's inside of? With code as shown below, I find that the text box is sized according to its content, and the popup is sized according to the textbox.

It works fine if I use fixed sizes in my row and column definitions, but I would like to popup to resize itself to match the user control (which in turn matches the browser).

<UserControl
  <!-- usual stuff here -->
>
<Grid>

<!-- layout for the user control here -->
<!-- and after that my popup: -->
<Popup Name="MyPopup">
   <Border BorderBrush="Black" BorderThickness="2" >
      <Grid>
         <Grid.RowDefinitions>
            <RowDefinition Height="22"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
         </Grid.RowDefinitions>
         <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"></ColumnDefinition>
         </Grid.ColumnDefinitions>

         <TextBlock Grid.Row="0" Text="Preview:" Margin="5" ></TextBlock>
         <TextBox 
              Grid.Row="1"
              Name="MyTextBox" 
              IsReadOnly="True"
              HorizontalScrollBarVisibility="Visible"
              VerticalScrollBarVisibility="Visible"
              TextWrapping="Wrap"
              Margin="5"
              >
          </TextBox>
      </Grid>
  </Border>
</Popup>

</Grid></UserControl>
+1  A: 

The C# code to do this would be something like:

    textBox1.Width = UserControl.Width;
    textBox1.Height = UserControl.Height;
    textBox1.Margin = UserControl.Margin;

The key here is resetting the margin. I know this works in WPF to, say, fill a Window with a TextBox. Give it a try, see if it works.

Rooke
It's strange, my Width and Height are both 'NaN', while my ActualWidth and ActualHeight seem kind of random. Guess I need to go read about the layout system. However I can confirm that I get different results when I set the Margin property as you suggest, thanks for that tip.
Eric

related questions