|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
IntroductionThis code allows you to hookup a popup control to any trigger within a Silverlight application. It prevents the developer from having to rewrite the same event handlers and timers that such controls need. It also gives a brief example of just how exactly to use the Popup primitive, but only so much as to get the purpose of the code across. I have called the code a BackgroundWhen I started working with the Using the codeThe code is very quick and easy to use. First create a simple Silverlight user control that contains a <UserControl x:Class="Example.Controls"
xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<StackPanel x:Name="LayoutRoot" Orientation="Horizontal">
<HyperlinkButton x:Name="TriggerButton" Content="Trigger"/>
<Popup x:Name="PopupControl">
<TextBlock x:Name="ChildText" Text="Popup Text"/>
</Popup>
</StackPanel>
</UserControl>
Next, add a private field to the code behind to keep a handle on the public ExampleControl()
{
InitializeComponent();
_popupProvider = new PopupProvider(TriggerButton, TriggerButton, PopupControl, ChildText, Direction.Bottom);
}
private PopupProvider _popupProvider;
Notes on the constructor parameters (these are also available in the source file). The parameters below appear in the order that they are used in the constructor. owner - The owner is the trigger - The trigger is the popup - The popup is the popupChild - The popupChild is the child control of the popup panel. The Popup control does not raise placement - Determines which side of the owner element the popup will appear on. This is an enum that is also included in the source code. Points of InterestThe provider code itself is mostly just attaching events and handling state that is not too insteresting. Probably the most valuable logic that I can share from within it is how the popup control is positioned. FrameworkElement page = Application.Current.RootVisual as FrameworkElement;
GeneralTransform gt = _owner.TransformToVisual(page);
Point p;
p = gt.Transform(new Point(0, _owner.ActualHeight - 2));
_popup.VerticalOffset = p.Y;
_popup.HorizontalOffset = p.X;
_popup.IsOpen = true;
There is obviously some room for error handling in there, but you get the point. Also, I have some words of warning. I haven't used this control in a terribly wide variety of situations yet. I know there are several bugs to be fixed, other events that could be handled and many usability tweaks to be made. If you have any suggestions, let me know or feel free to implement them yourself. History2008-05-23 Made a couple of spelling fixes and added some description about the provider code.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||