タイトルが曖昧ではないことを願っています。私がしようとしていることは(基本的な例として)キャンバスにプログラムで矩形を追加し、後でいくつかの点でローカル変数を異なるプロパティを持つ新しい矩形に変更し、その更新をキャンバス上で行います。ローカル変数値が新しいUIElementに設定されているときに、プログラムで作成されたUIElement 'unlinks'
// First rectangle
Rectangle rect = new Rectangle()
{
Width = 50,
Height = 50,
Fill = Brushes.Red,
Margin = new Thickness(20, 20, 0, 0)
};
// Add it to the canvas
mainCanvas.Children.Add(rect);
// Change something about the rectangle, which works
rect.Fill = Brushes.Black;
// Create new rectangle
Rectangle newRect = new Rectangle()
{
Width = 15,
Height = 20,
Fill = Brushes.Blue,
Margin = new Thickness(20, 20, 0, 0)
};
// Set the original rectangle to the new rectangle
rect = newRect;
// Canvas rectangle is no longer 'linked' to the rect variable :(
それでした!それはかなりシンプルで、変数は別のクラスに格納され、メソッドを介してそのクラスに再割り当てされたので、キャンバスにアクセスすることはできませんでした。私はパラメータとしてキャンバスを渡し、あなたが提案したコードを使用しました。乾杯! – Aidan