在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
.NET支持的类型参数约束有以下五种: 如下在WPF中获取当前窗体(或者用户控件)的父窗体,限定泛型必须为Class: private void btnCancel_Click(object sender, RoutedEventArgs e) { Window hostWindow = GetParent<Window>(sender as Button); if (hostWindow != null) { hostWindow.Close(); } } public static T GetParent<T>(UIElement uiElement) where T : class { T result; var dp = LogicalTreeHelper.GetParent(uiElement); result = dp as T; while (result == null) { dp = LogicalTreeHelper.GetParent(dp); result = dp as T; if (dp == null) return null; } return result; } .NET支持的类型参数约束有以下五种: 如下在WPF中获取当前窗体(或者用户控件)的父窗体,限定泛型必须为Class: private void btnCancel_Click(object sender, RoutedEventArgs e) { Window hostWindow = GetParent<Window>(sender as Button); if (hostWindow != null) { hostWindow.Close(); } } public static T GetParent<T>(UIElement uiElement) where T : class { T result; var dp = LogicalTreeHelper.GetParent(uiElement); result = dp as T; while (result == null) { dp = LogicalTreeHelper.GetParent(dp); result = dp as T; if (dp == null) return null; } return result; } |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论