(Canvas problem) Re: Silverlight 2: Canvas visibility change not applied until user moves mouse over controls
Hi Davd Relyea again,
Like you suggested in another post, I am porting my code to use two-pass layout system by deriving Panel class, or try to use default layout containers as much as I can. However I still need many portion of my code using Canvas as they rely on x-y coordinates and items are depending on the size of Canvas (perhabs I should inherit Canvas and use OverrideArrange to see what size is coming along?). I am just trying to mix all layout methods I can.
Problem 1: I was testing Canvas objects again and again and realized that Canvas itself doesn’t appear if it contains any layout elements in it. Very much similar to AdamJTP’s problem
Problem 2: When I have nested SizeChange events as I have nested Canvases. It only seem to fire SizeChanged event for first few Canvases. May be I am doing something wrong here, your advice would be greatly appriciated.
I just wish to make my application up and running again soon. Below is my test program
Thanks a lot for your help
App.xaml.cs
public partial class App : Application{
private Page root;
public App()
{ this.Startup += this.Application_Startup;
this.Host.Content.Resized += new EventHandler(Content_Resized);
InitializeComponent();
}
private void Application_Startup(object sender, StartupEventArgs e)
{
this.root = new Page();
this.RootVisual = this.root;
}
void Content_Resized(object sender, EventArgs e)
{
System.Diagnostics.Debug.WriteLine(string.Format("Host Size {0} {1}", this.Host.Content.ActualWidth, this.Host.Content.ActualHeight));
this.root.Width = this.Host.Content.ActualWidth;
this.root.Height = this.Host.Content.ActualHeight;
}
}
Page.xaml. Uncomment two layout elements to see the effect for the problem 1. Green color should never display but it does.
<UserControl x:Class="CanvasTest.Page"
xmlns=http://schemas.microsoft.com/client/2007
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="500" Height="500">
<Canvas x:Name="topCanvas" Background="Green">
<Canvas x:Name="leftCanvas" Background="Red">
<Canvas x:Name="leftCanvasChild" Background="Blue">
<!--<Button x:Name="button1" Content="xyz"/>-->
</Canvas>
</Canvas>
<Canvas x:Name="rightCanvas" Background="Magenta">
<Canvas x:Name="rightCanvasChild" Background="Cyan">
<!--<TextBox x:Name="textBox" Text="Some text"/>-->
</Canvas>
</Canvas>
</Canvas>
</UserControl>
Page.xaml.cs
public partial class Page : UserControl
{ public Page()
{
InitializeComponent();
this.SizeChanged += new SizeChangedEventHandler(OnPageSizeChanged);
this.topCanvas.SizeChanged += new SizeChangedEventHandler(topCanvas_SizeChanged);
this.leftCanvas.SizeChanged += new SizeChangedEventHandler(leftCanvasSizeChanged);
this.rightCanvas.SizeChanged += new SizeChangedEventHandler(rightCanvas_SizeChanged);
} private void OnPageSizeChanged(object sender, SizeChangedEventArgs e)
{
System.Diagnostics.Debug.WriteLine("Page Size {0},{1}", e.NewSize.Width, e.NewSize.Height);
this.topCanvas.Width = e.NewSize.Width;
this.topCanvas.Height = e.NewSize.Height;
}
void topCanvas_SizeChanged(object sender, SizeChangedEventArgs e){
System.Diagnostics.Debug.WriteLine("TopCanvas Size {0},{1}", e.NewSize.Width, e.NewSize.Height);
this.leftCanvas.Width = e.NewSize.Width / 2;
this.leftCanvas.Height = e.NewSize.Height;
Canvas.SetLeft(this.rightCanvas, e.NewSize.Width / 2);
this.rightCanvas.Width = e.NewSize.Width / 2;
this.rightCanvas.Height = e.NewSize.Height;
//Desired effect for the leftCanvasChild and rightCanvasChild.
//this.leftCanvasChild.Width = e.NewSize.Width / 4;
//this.leftCanvasChild.Height = e.NewSize.Height;
//this.rightCanvasChild.Width = e.NewSize.Width / 4;
//this.rightCanvasChild.Height = e.NewSize.Height;
}
void leftCanvasSizeChanged(object sender, SizeChangedEventArgs e)
{//this never gets called
System.Diagnostics.Debug.WriteLine("LeftCanvas Size {0},{1}", e.NewSize.Width, e.NewSize.Height);
this.leftCanvasChild.Width = e.NewSize.Width / 2;
this.leftCanvasChild.Height = e.NewSize.Height;
}
void rightCanvas_SizeChanged(object sender, SizeChangedEventArgs e)
{
//never reach here.
System.Diagnostics.Debug.WriteLine("RightCanvas Size {0},{1}", e.NewSize.Width, e.NewSize.Height);
this.rightCanvasChild.Width = e.NewSize.Width / 2;
this.rightCanvasChild.Height = e.NewSize.Height;
}
}