我在运行时使用矩形和拇指作为孩子创建画布。 我想访问父级别,我的父级是发件人事件中的画布,由其子级调用。 Canvas是在运行时创建的。在变量p中,我想访问父亲的身高。
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);
}
}
答案 0 :(得分:0)
因为您知道Thumb的父级是Canvas,所以您可以将其转换为Canvas类型&然后获取它的尺寸。
Canvas parentOfThumb = thumbSender.Parent as Canvas;
if (parentOfThumb != null) //if in case Parent is not canvas
int p = parentOfThumb.ActualHeight;