ボタンによって呼び出されるメソッドを作成しています。次に、このメソッドはキャンバスをボタンの親コンテナー コントロールに追加します。たとえば、ボタンはグリッド上にあります。次に、このメソッドは、ボタンのすぐ下に表示されるキャンバスを作成します。しかし、私には2つの問題があります:
- ボタンの親コンテナへの参照を取得するにはどうすればよいですか?
- コンテナー コントロールのクラスはありますか? ボタンがグリッド、キャンバス、スタックパネルなどにあるかどうかは気にしません。そのため、すべてのタイプの contianers が実装するインターフェイスまたはそれらが継承するクラスを探しています。
コンテナへの参照を手動で渡すことができるため、2 番目の側面はより重要です。
編集:
これは次のようになります (色を除いて、これらは異なる要素を示すためだけのものです。
確認を処理するために赤いキャンバスがポップアップするはずです。たぶん、素敵なアニメーションでも。私の考えは、次のように呼び出すことができるクラスを作成することでした:
MyPopup popup = new MyPopup("Are you sure?", "Yes", "No", delegateFirstButton, delegateSecondButton);
popup.Show();
これまでのコードはまだクラスではなく、メソッドのみです。テキスト部分は、現時点ではハードコードされています。マークされた行はより柔軟にする必要があり、それが私の質問の理由です。
public void ShowPopup(Control senderControl)
{
//I need to have a parameter that accepts all containers instead of this line:
this.myGrid.Children.Add(popup);
Border border = new Border();
popup.Children.Add(border);
border.Margin = new Thickness() { Top = 10 };
border.Child= text;
text.Text = "Are you sure?";
text.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
popup.SizeChanged += delegate { border.Width = popup.ActualWidth; };
popup.Children.Add(btn1);
btn1.Content = "Yes";
btn1.Height = 22;
btn1.Padding = new Thickness(10, 0, 10, 0);
btn1.Margin = new Thickness() { Left = 15, Top = 35 };
popup.Children.Add(btn2);
btn2.Content = "No";
btn2.Height = 22;
btn2.Padding = new Thickness(10, 0, 10, 0);
btn1.SizeChanged += delegate { btn2.Margin = new Thickness() { Left = 30 + btn1.ActualWidth, Top = 35 }; };
popup.Height = 70;
btn2.SizeChanged += delegate
{
popup.Width = 45 + btn1.ActualWidth + btn2.ActualWidth;
updatePositions(senderControl);
};
popup.Background = Brushes.Red;
popup.VerticalAlignment = System.Windows.VerticalAlignment.Top;
popup.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
}
public void updatePositions(Control senderControl)
{
Point location = senderControl.TranslatePoint(new Point(0, 0), this.myGrid);
popup.Margin = new Thickness()
{
Left = location.X + (senderControl.ActualWidth / 2) - (popup.Width / 2),
Top = location.Y + senderControl.ActualHeight + 15
};
}