Skip to content

Map AttachedProperties

23 June 2008

Currently I’m trying to implement a reusable control for reflection in WPF. I come to the conclusion that XAML is a very easy way to achieve such an effect but it is not the ‘modern abstract’ way. So after a while I found a very nice example which operates with AttachedProperties to display a reflection (see Reflecting on Reflections).

But the author missed to copy all AttachedProperties for a Panel from the element that has reflection  to the new Panel (which is responsible for the reflection at last).

Here you can find a first and rapid implementation to gain all necessary AttachedProperties for Panels:

public static class AttachedPropertiesManager
{
private static FlowDirection FlowDirection;
private static int ZIndex;
private static Dock Dock;
private static readonly CanvasAttachedPropertiesManager CanvasAttachedProperties = new CanvasAttachedPropertiesManager();
private static readonly GridAttachedPropertiesManager GridAttachedProperties = new GridAttachedPropertiesManager();

/// <summary>
/// Saves all AttachedProperties of an element within a Panel
/// </summary>
/// <remarks>Canvas, DockPanel and Grid are the only Panel derived classes which implement AttachedProperties.</remarks>
/// <param name=”element”>Element that holds AttachedProperties</param>
Element that holds AttachedProperties public static void SaveAttachedProperties(FrameworkElement element)
{
if (element == null) throw new ArgumentNullException(“element”);
// General AttachedProperties
FlowDirection = FrameworkElement.GetFlowDirection(element);
ZIndex = Panel.GetZIndex(element);
// Canvas AttachedProperties
CanvasAttachedProperties.Top = Canvas.GetTop(element);
CanvasAttachedProperties.Bottom = Canvas.GetBottom(element);
CanvasAttachedProperties.Left = Canvas.GetLeft(element);
CanvasAttachedProperties.Right = Canvas.GetRight(element);
// DockPanel AttachedProperties
Dock = DockPanel.GetDock(element);
// Grid AttachedProperties
GridAttachedProperties.Column = Grid.GetColumn(element);
GridAttachedProperties.ColumnSpan = Grid.GetColumnSpan(element);
GridAttachedProperties.Row = Grid.GetRow(element);
GridAttachedProperties.RowSpan = Grid.GetRowSpan(element);
GridAttachedProperties.IsSharedSizeScope = Grid.GetIsSharedSizeScope(element);
}
/// <summary>
/// Restores all AttachedProperties of an element within a Panel
/// </summary>
/// <remarks>Canvas, DockPanel and Grid are the only Panel derived classes which implement AttachedProperties.</remarks>
/// <param name=”element”>Element that will implement all current AttachedProperties.</param>
Element that will implement all current AttachedProperties. public static void RestoreAttachedProperties(UIElement element)
{
if (element == null) throw new ArgumentNullException(“element”);
// General AttachedProperties
FrameworkElement.SetFlowDirection(element, FlowDirection);
Panel.SetZIndex(element, ZIndex);
// Canvas AttachedProperties
Canvas.SetTop(element, CanvasAttachedProperties.Top);
Canvas.SetBottom(element, CanvasAttachedProperties.Bottom);
Canvas.SetLeft(element, CanvasAttachedProperties.Left);
Canvas.SetRight(element, CanvasAttachedProperties.Right);
// DockPanel AttachedProperties
DockPanel.SetDock(element, Dock);
// Grid AttachedProperties
Grid.SetColumn(element, GridAttachedProperties.Column);
Grid.SetColumnSpan(element, GridAttachedProperties.ColumnSpan);
Grid.SetRow(element, GridAttachedProperties.Row);
Grid.SetRowSpan(element, GridAttachedProperties.RowSpan);
Grid.SetIsSharedSizeScope(element, GridAttachedProperties.IsSharedSizeScope);
}

public class GridAttachedPropertiesManager
{
public int Column;
public int ColumnSpan;
public int Row;
public int RowSpan;
public bool IsSharedSizeScope;
}

public class CanvasAttachedPropertiesManager
{
public double Top;
public double Bottom;
public double Left;
public double Right;
}
}

Advertisements

From → .NET, WPF

Leave a Comment

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: