There is a custom control that extends from Canvas
and can only accept instances of the class Shape
as its children. Consider the code below:
public class SvgGroup : Canvas
{
// ...
public Brush Fill
{
// Retuns the fill brush value of all the shape children, if they are all the same. Otherwise, the default value of Brush is returned
get
{
Brush rtn = default(Brush);
for (int i = 0; i < ShapeChildren.Count; i++)
{
Shape shape = ShapeChildren[i];
if (i == 0) // First loop
{
rtn = shape.Fill;
}
else if (rtn != shape.Fill) // Children shapes have different Fill value
{
return default(Brush);
}
}
return rtn;
}
// Sets the fill brush value of all the shape children
set
{
foreach (Shape shape in ShapeChildren)
{
shape.Fill = value;
}
}
}
// ...
}
The problem is when setting the Fill property in XAML, nothing happens. However setting the Fill in code-behind works.
I was thinking of dependency properties, but the implementation in this scenario could be quite tricky.