【问题标题】:CommonOpenFileDialog allows usage of parent windowCommonOpenFileDialog 允许使用父窗口
【发布时间】:2021-02-16 13:03:34
【问题描述】:

您好,我的应用程序使用一些OpenFileDialogs 进行文件选择。此外,我需要一个文件夹选择器,为此我使用了 CommonOpenFileDialog 和选项 IsFolderPicker = true

现在,当我在应用程序中打开 OpenFileDialog 时,父窗口被锁定并且不能再使用,这正是我想要的行为。 但是当我使用CommonOpenFileDialog 时,我仍然可以访问父窗口并打开更多CommonOpenFileDialogs。

OpenFileDialog 是这样初始化的:

//init dialog
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.InitialDirectory = Path.GetFullPath(SecondQPcrFilePath);
openFileDialog.CheckFileExists = true;
openFileDialog.CheckPathExists = true;
openFileDialog.Multiselect = false;
openFileDialog.ReadOnlyChecked = false;
openFileDialog.ShowReadOnly = false;

//show dialog
bool? dialogResult = openFileDialog.ShowDialog();

CommonOpenFileDialog 是这样的:

//is this mvvm conform?
Button senderButton = (Button)sender;
string clickedButton = senderButton.Name;
//init dialog
CommonOpenFileDialog openPathDialog = new CommonOpenFileDialog();

openPathDialog.IsFolderPicker = true;
openPathDialog.EnsurePathExists= true;
openPathDialog.Multiselect = false;
openPathDialog.EnsureFileExists = false;
openPathDialog.AllowNonFileSystemItems = true;

if(clickedButton == "limsOpenButton")
{
    openPathDialog.InitialDirectory = Path.GetFullPath(LimsPath);
}
else if(clickedButton == "qpcrOpenButton")
{
    openPathDialog.InitialDirectory = Path.GetFullPath(QpcrPath);
}
//show dialog
CommonFileDialogResult dialogResult = openPathDialog.ShowDialog();

有没有办法防止这种行为?普通对话框没有 Owner 或类似的属性。

【问题讨论】:

    标签: c# wpf


    【解决方案1】:

    公用文件对话框公开了ShowDialog 方法的重载。

    public CommonFileDialogResult ShowDialog(Window window);
    

    将父窗口传递给此方法,对话框将是模态的。

    var window = // Get the parent window here.
    var dialogResult = openPathDialog.ShowDialog(window);
    

    看起来您使用了代码隐藏,所以this 将是窗口。或者,您可以使用 MainWindowApplication.Current 中的 Windows 集合来查找窗口。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-27
      • 1970-01-01
      • 1970-01-01
      • 2016-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多