WPF 获得控件的父控件

通过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);
        }
<pre id="best-content-1141229721" accuse="aContent" class="best-text mb-10" style="margin-top: 0px; margin-bottom: 10px; padding: 0px; font-family: arial, 'courier new', courier, 宋体, monospace, 'Microsoft YaHei'; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); line-height: 24px; background-color: rgb(243, 255, 236);">然后你断点看下a,就会发现它就是button的父容器myGrid





                
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值