Reputation: 1125
I'm an experienced programmer, but completely new to the world of Silverlight. I've been tasked with fixing a problem in a Silverlight application, and am struggling to find the information I need.
The app contains a custom control, upon which pop-up tooltips can be displayed near the mouse cursor. I need to ensure the tooltips get positioned in such a way that they don't exceed the bounds of the control. Easy, in theory, however I can't figure out how to get the size of the control.
It's declared in some XAML, like this (main control name changed to 'XXX' for privacy):
<client:XxxHost x:Class="Project.Client.Banana"
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"
xmlns:xxx="clr-namespace:Project.Controls;assembly=Project.Controls.Xxx"
xmlns:ui="clr-namespace:Project.Controls.UI;assembly=Project.Controls.UI"
xmlns:client="clr-namespace:Project.Client"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<xxx:XxxControl x:Name="XxxControl">
<xxx:XxxControl.UI>
<ui:Banana DataContext="{Binding}" View="{Binding View}" />
</xxx:XxxControl.UI>
<xxx:XxxControl.TooltipDisplay>
<xxx:MultiTooltipDisplay></xxx:MultiTooltipDisplay>
</xxx:XxxControl.TooltipDisplay>
</xxx:XxxControl>
</client:XxxHost>
In the code, MultiTooltipDisplay is a class derived from Canvas. In its method to display a tooltip, it has this code:
public void DisplayTooltips(System.Collections.Generic.List<UIElement> tooltips, Point screenAnchor)
{
ClearTooltips();
if (tooltips.Count > 0)
{
StackPanel ttStack = new StackPanel();
ttStack.Orientation = Orientation.Vertical;
for (int i = 0; i < tooltips.Count; i++)
{
UIElement ttip = tooltips[ i ];
UserControl ttipWrapper = new UserControl();
ttipWrapper.Content = ttip;
ttStack.Children.Add(ttipWrapper);
}
ttStack.SetValue(Canvas.TopProperty, screenAnchor.Y);
ttStack.SetValue(Canvas.LeftProperty, screenAnchor.X + 24.0);
this.Children.Add(ttStack);
}
}
If I put a breakpoint in towards the end of this method, and I inspect ttStack's Width/Height/ActualWidth/ActualHeight/DesiredSize
, I find they're all 0 or NaN. After some reading and trial & error I found out that by putting in a call to ttStack.UpdateLayout() those values would get populated. I'm not sure if that's the correct way, but the numbers seem reasonable. Great!
However, I need to know the size of the control upon which the tooltip would be displayed, but I get the same issue. this.Width/Height/etc all return 0 or NaN. A call to this.UpdateLayout() doesn't change those properties.
How can I determine the dimensions of the contol?
Upvotes: 2
Views: 1765
Reputation: 189525
Try calling the Measure
method on ttStack before reading is DesiredSize
.
Upvotes: 1