I was creating a user control and encountered some strange drawing
behavior. I boiled it down to a fairly small piece of code that I post
at the bottom of this e-mail.

The code draws a 10x10 canvas with a bright yellow background. It's
later programmatically resized to be much larger, but the change
doesn't seem to take effect, even though other portions of the window
are updated. For some reason, if I take out an unrelated Grid control,
it draws fine. Also, if I reference the code earlier, it draws
correctly (in Load instead of SizeChanged). Do I need to make a call
to have the changes take effect? UpdateLayout() doesn't appear to
help.

Following is the code:

<UserControl x:Class="TestApp.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation";
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml";
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008";
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/
2006"
    mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480"
    Loaded="MainPage_Loaded" SizeChanged="MainPage_SizeChanged">
 <Canvas x:Name="m_MainCanvas" Background="Blue">
  <Canvas Name="m_NestedCanvas" Background="Yellow" Canvas.Left="0"
Canvas.Top="0" Width="10" Height="10">
   <!-- Following is the grid, that if removed, allows the above
canvas to draw fine. -->
   <Grid Canvas.Left="594" Canvas.Top="80" Width="10" Height="400"
Background="Red">
   </Grid>
  </Canvas>
 </Canvas>
</UserControl>


Code-behind:

  private void MainPage_Loaded(object sender, RoutedEventArgs e)
  {
   // Enabling the following causes the window to draw fine.
   /*m_NestedCanvas.Width = 680;
   m_NestedCanvas.Height = 443;
   Canvas.SetTop(m_NestedCanvas, 110);
   Canvas.SetLeft(m_NestedCanvas, 84);*/
  }

  private void MainPage_SizeChanged(object sender,
SizeChangedEventArgs e)
  {
   m_NestedCanvas.Width = 680;
   m_NestedCanvas.Height = 443;
   Canvas.SetTop(m_NestedCanvas, 110);
   Canvas.SetLeft(m_NestedCanvas, 84);
  }



TIA!

Reply via email to