在WPF Visual Tree中查找父控件

实现

csharp 复制代码
    private static T FindVisualParent<T>(DependencyObject child)
        where T : DependencyObject
    {
        DependencyObject parentObject = VisualTreeHelper.GetParent(child);
        if (parentObject == null)
        {
            return null;
        }

        T parent = parentObject as T;
        if (parent != null)
        {
            return parent;
        }
        
        return FindVisualParent<T>(parentObject);
    }

使用

csharp 复制代码
 var userControl = FindVisualParent<UserControl>(AssociatedObject);
相关推荐
廋到被风吹走15 小时前
【AI】Codex 复杂任务拆解:从“一气呵成“到“步步为营“
人工智能·wpf
希望永不加班15 小时前
SpringBoot 整合 Redis 缓存
spring boot·redis·后端·缓存·wpf
_MyFavorite_15 小时前
JAVA重点基础、进阶知识及易错点总结(29)JDK8 时间 API 进阶
java·开发语言·wpf
武藤一雄2 天前
深入拆解.NET内存管理:从GC机制到高性能内存优化
windows·microsoft·c#·.net·wpf·.netcore·内存管理
武藤一雄4 天前
WPF/C# 应对消息洪峰与数据抖动的 8 种“抗压”策略
windows·微软·c#·wpf·.netcore·防抖·鲁棒性
武藤一雄5 天前
WPF深度解析Behavior
windows·c#·.net·wpf·.netcore
Maybe_ch5 天前
WPF的STA线程模型、APM与TAP:从线程约束到现代异步
c#·.net·wpf
FuckPatience5 天前
WPF 实现windows文件压缩文件解压过程动画
wpf
会飞的大可5 天前
Spring Cloud Alibaba全景:Nacos、Sentinel、Seata整合实战
sentinel·wpf
baivfhpwxf20235 天前
DataGrid 中增加选择列 功能实现
ui·wpf