Canvas
から拡張されたカスタムコントロールがあり、その子としてクラスShape
のインスタンスのみを受け入れることができます。以下のコードを検討してください。XAMLでプロパティセッターが動作しない
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;
}
}
}
// ...
}
XAMLでFillプロパティを設定すると何も起こりません。ただし、コードビハインドの塗りつぶしの設定は動作します。
私は依存関係のプロパティを考えていましたが、このシナリオの実装は非常に難しいかもしれません。
。 – Clemens
true、これはXAMLが解析されたときに発生します。これは私がDependencyPropertyについて考えていた理由です –
どうすればそれが役に立ちますか? Fillが設定された後も、追加された子要素に反応する必要があります。 – Clemens