-
Nov 29th, 2023, 08:13 PM
#1
Thread Starter
New Member
How do I force a canvas on an unviewed tab to have a size set in advance?
I'm working on an application that has two different tabs on the main window. Each tab has it's own canvas as a child object. When the tab selection is changed I redraw the canvas on that tab.
The problem is, when a user switches to the second tab for the first tab, the "tab changed" event fires before the canvas on the second tab has been assigned a size (debugging shows it has a size of 0,0). As such, the custom elements I add to it don't draw correctly since they are size dependent. If it redraws after the tab has been opened already it works fine since the size has been set by that point.
Can someone please suggest a way to have the size set before the tab is first selected or is there some other trick I can use?
-
Dec 10th, 2023, 02:50 PM
#2
Re: How do I force a canvas on an unviewed tab to have a size set in advance?
One way, if the canvas has to fill the whole tab, you could set HorizontalAlignment and VerticalAlignment properties of the Canvas to Stretch.
Code:
<TabControl>
<TabItem Header="Tab 1">
<Canvas Background="LightGray" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<!-- Your canvas content here -->
</Canvas>
</TabItem>
<TabItem Header="Tab 2">
<Canvas Background="LightBlue" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<!-- Your canvas content here -->
</Canvas>
</TabItem>
</TabControl>
-
Dec 10th, 2023, 02:55 PM
#3
Re: How do I force a canvas on an unviewed tab to have a size set in advance?
Or to ensure the size is set before the tab is first selected, you could try this:
Code:
Private Sub Canvas_SizeChanged(sender As Object, e As SizeChangedEventArgs)
Dim canvas As Canvas = DirectCast(sender, Canvas)
' Check if the canvas is initialized and has a non-zero size
If canvas.IsInitialized AndAlso canvas.ActualWidth > 0 AndAlso canvas.ActualHeight > 0 Then
' Execute your logic for drawing or updating custom elements
UpdateCanvas(canvas)
End If
End Sub
Private Sub UpdateCanvas(canvas As Canvas)
' Your logic for drawing or updating custom elements goes here
End Sub
Make sure to subscribe to the SizeChanged event for each canvas in your code:
Code:
<Canvas x:Name="FirstTabCanvas" SizeChanged="Canvas_SizeChanged">
<!-- Your canvas content here -->
</Canvas>
<Canvas x:Name="SecondTabCanvas" SizeChanged="Canvas_SizeChanged">
<!-- Your canvas content here -->
</Canvas>
Last edited by Peter Porter; Dec 10th, 2023 at 03:01 PM.
-
Dec 10th, 2023, 03:03 PM
#4
Thread Starter
New Member
Re: How do I force a canvas on an unviewed tab to have a size set in advance?
OK, I'll give that a try. Thanks.
-
Nov 9th, 2024, 02:14 AM
#5
New Member
Great topic for discussion
Great topic for discussion, thanks for bringing this up!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|