Windows 8 Metro 用のアプリを作成していますが、Canvas の親子関係がどのように機能するかを理解するのに問題があります。あるキャンバスを別のキャンバスの子に追加すると、親キャンバスがビジュアル ツリーに追加されるまで、サブキャンバスの ->parent プロパティが null に等しいようです。以下は、問題を示すサンプル コードです。
Xaml::Controls::Canvas^ testCanvas = ref new Xaml::Controls::Canvas();
Xaml::Controls::Canvas^ childCanvas = ref new Xaml::Controls::Canvas();
testCanvas->Children->Append(childCanvas);
int size = testCanvas->Children->Size; // size will be equal to 1
if(!childCanvas->Parent)
{
// this code is executed because ->Parent is null. Why is parent here null??
}
aCanvasThatIsPartOfTheVisualTree->Children->Append(testCanvas);
if(!childCanvas->Parent)
{
// this code does not get executed because ->Parent is not null, now that testCanvas is part of the visual tree.
}
また、VisualTreeHelper を使用して親を取得しようとしました。
Xaml::Controls::Canvas^ theParent = (Xaml::Controls::Canvas^)VisualTreeHelper::GetParent(childCanvas);
動作はそのままです。theParent は、testCanvas がビジュアル ツリーに追加される前は null であり、testCanvas がビジュアル ツリーに追加された後は null ではありません。
これがバグでない場合、親がビジュアル ツリーの一部にならずにサブキャンバスの親を取得する方法を誰か教えてもらえますか? バグの場合、既知の回避策はありますか? ありがとうございました!