In Silverlight, the Canvas class (with Reflector) has a very simple implementation: 3 attached dependency properties (Left, Top, ZIndex) and 2 ovverides of the MeasureOverride and ArrangeOverride methods, which do nothing special.
But if I use my implementation, for example:
class MyCanvas : Panel { }
And then use MyCanvas in XAML, like a standard Canvas. This does not work as expected (I see a blank screen).
How was Canvas implemented?
- Additional code: MyCanvas.cs
public class MyCanvas : Panel
{
public static double GetTop(DependencyObject obj)
{
return (double)obj.GetValue(TopProperty);
}
public static void SetTop(DependencyObject obj, double value)
{
obj.SetValue(TopProperty, value);
}
public static readonly DependencyProperty TopProperty =
DependencyProperty.RegisterAttached("Top", typeof(double), typeof(MyCanvas), new PropertyMetadata(0.0));
public static double GetLeft(DependencyObject obj)
{
return (double)obj.GetValue(LeftProperty);
}
public static void SetLeft(DependencyObject obj, double value)
{
obj.SetValue(LeftProperty, value);
}
public static readonly DependencyProperty LeftProperty =
DependencyProperty.RegisterAttached("Left", typeof(double), typeof(MyCanvas), new PropertyMetadata(0.0));
}
Used in XAML, for example:
<local:MyCanvas>
<Rectangle
local:MyCanvas.Left="10"
local:MyCanvas.Top="10"
Width="100"
Height="100"
Fill="Black" />
</local:MyCanvas>
if I change MyCanvas to a standard canvas, I can view the rectangle with black placeholder at position 10.10.
<Canvas>
<Rectangle
Canvas.Left="10"
Canvas.Top="10"
Width="100"
Height="100"
Fill="Black" />
</Canvas>