C# 如何使用TryFindParent
我正在使用Mahapps metro编写一个应用程序。自定义弹出对话框源自“窗口”,其属性的“所有者”设置为主窗口。在该对话框中,TryFindParent的结果始终为空C# 如何使用TryFindParent,c#,wpf,mahapps.metro,C#,Wpf,Mahapps.metro,我正在使用Mahapps metro编写一个应用程序。自定义弹出对话框源自“窗口”,其属性的“所有者”设置为主窗口。在该对话框中,TryFindParent的结果始终为空 public class CustomDialog : Window { public async void doItButton_Click(object sender, RoutedEventArgs e) { var parent = await this.TryFindParent<MainWindo
public class CustomDialog : Window {
public async void doItButton_Click(object sender, RoutedEventArgs e) {
var parent = await this.TryFindParent<MainWindow>();
if (null != parent) // parent is null.
await parent.ShowBusyIndicator();
}
}
public class MainWindow : MetroWindow {
public void showPopup_Click(object sender, RoutedEventArgs e) {
CustomDialog dlg = new CustomDialog();
dlg.Owner = this;
var res = dlg.ShowDialog();
if (null != res && !res.Value) return;
MessageBox.Show("");
}
public async Task<ProgressDailogController> ShowBusyIndicator() {
//TODO
}
}
公共类自定义对话框:窗口{
public async void doItButton_Click(对象发送方,RoutedEventTargets e){
var parent=wait this.TryFindParent();
如果(null!=parent)//parent为null。
等待parent.ShowBusyIndicator();
}
}
公共类主窗口:MetroWindow{
公共无效显示弹出窗口单击(对象发送者,路由目标){
CustomDialog dlg=新建CustomDialog();
dlg.Owner=这个;
var res=dlg.ShowDialog();
if(null!=res&!res.Value)返回;
MessageBox.Show(“”);
}
公共异步任务ShowBusyIndicator(){
//待办事项
}
}
还有一点,如何在显示的自定义弹出对话框上禁用动画?一个
窗口没有可视的父窗口,但由于您设置了自定义对话框的所有者
属性,您可以将其强制转换以获得对“父窗口”的引用:
要禁用MetroWindow
的动画,您应该能够将WindowTransitionsEnabled
属性设置为false
,因为窗口没有父窗口。您必须搜索窗口的所有者属性。我想知道如何使用“TryFindParent”。我查了业主的房子,谢谢。您的意思是TryFindParent用于Combobox之类的控件吗?它是一种在可视树中查找父元素的方法,但如上所述,窗口没有父元素。
public async void doItButton_Click(object sender, RoutedEventArgs e) {
var parent = this.Owner as MainWindow;
if (null != parent)
await parent.ShowBusyIndicator();
}