I am creating canvas at run-time with rectangle and thumb as a child. I want to access parent height,my parent is a canvas in a sender event which is called by its child. Canvas is created at run-time. In variable p I want to access parent's height.
private void Rectangle_Click(object sender, RoutedEventArgs e)
{
var canvas = new Canvas();
canvas.Height = 100;
canvas.Width = 100;
canvas.Background = new SolidColorBrush(Colors.Violet);
var thumb = new Thumb();
thumb.Background = new SolidColorBrush(Colors.LimeGreen);
var template = new ControlTemplate();
thumb.Height = 20;
thumb.Width = 20;
thumb.DragDelta+=thumb_DragDelta;
canvas.Children.Add(thumb);
Canvas.SetTop(thumb, 90);
Canvas.SetLeft(thumb, 90);
canvas.ManipulationMode = ManipulationModes.All;
canvas.ManipulationDelta += rec_ManipulationDelta;
canvas.RenderTransform = new TranslateTransform(); // Create new TranslateTransform and assign to the rectangle
board.Children.Add(canvas);
}
private void thumb_DragDelta(object sender, DragDeltaEventArgs e)
{
Thumb thumbSender = (Thumb)sender;
var p= thumbSender.Parent;
//double yadjust = recSender.Height + e.VerticalChange;
//double xadjust = recSender.Width + e.HorizontalChange;
//if ((xadjust >= 0) && (yadjust >= 0))
{
//recSender.Width = xadjust;
//recSender.Height = yadjust;
Canvas.SetLeft(thumbSender, Canvas.GetLeft(thumbSender) + e.HorizontalChange);
Canvas.SetTop(thumbSender, Canvas.GetTop(thumbSender) + e.VerticalChange);
}
}
Since you know the parent of Thumb is Canvas you could cast it to the Canvas type & then fetch its dimensions.
Canvas parentOfThumb = thumbSender.Parent as Canvas;
if (parentOfThumb != null) //if in case Parent is not canvas
int p = parentOfThumb.ActualHeight;