wpf 路由事件如何获得子控件的父控件
1个回答
展开全部
通过VisualTreeHelper去取就行了。给你段代码参考:
前台:
<Grid Name="rootGrid">
<Grid Name="myGrid" Width="150" Height="150" Background="LightBlue">
<Button Name="btn" Click="btn_Click" Width="80" Height="25" Content="click"/>
</Grid>
</Grid>
后台:
private void btn_Click(object sender, RoutedEventArgs e)
{
Button btn = sender as Button;
var a = VisualTreeHelper.GetParent(btn);
}
然后你断点看下a,就会发现它就是button的父容器myGrid
前台:
<Grid Name="rootGrid">
<Grid Name="myGrid" Width="150" Height="150" Background="LightBlue">
<Button Name="btn" Click="btn_Click" Width="80" Height="25" Content="click"/>
</Grid>
</Grid>
后台:
private void btn_Click(object sender, RoutedEventArgs e)
{
Button btn = sender as Button;
var a = VisualTreeHelper.GetParent(btn);
}
然后你断点看下a,就会发现它就是button的父容器myGrid
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询