C# 依赖项属性作为对话框初始目录多线程问题

C# 依赖项属性作为对话框初始目录多线程问题,c#,wpf,multithreading,dialog,dependency-properties,C#,Wpf,Multithreading,Dialog,Dependency Properties,我正在制作一个文件选择器UserControl,使用依赖项属性存储一个字符串,该字符串表示打开的文件对话框的初始目录: public static readonly DependencyProperty DefaultFolderProperty = DependencyProperty.Register("DefaultFolder", typeof(string), typeof(FileSelector), new PropertyMetadata(Path.GetDirector

我正在制作一个文件选择器
UserControl
,使用依赖项属性存储一个字符串,该字符串表示打开的文件对话框的初始目录:

public static readonly DependencyProperty DefaultFolderProperty =
    DependencyProperty.Register("DefaultFolder", typeof(string), typeof(FileSelector), new PropertyMetadata(Path.GetDirectoryName(Directory.GetCurrentDirectory())));    

public string DefaultFolder
    {
        get { return (string)this.GetValue(DefaultFolderProperty); }
        set { this.SetValue(DefaultFolderProperty, value); }
    }
此属性绑定到我的
UserControl
中的
Textbox
,因此我可以手动写入目录地址:

<TextBox x:Name="SelectedFolder" Style="{DynamicResource BaseTextBoxStyle}" FontSize="8" Grid.Column="3" Grid.ColumnSpan="2" Text="{Binding DataContext.DefaultFolder, ElementName=F_Selector, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
现在,当我使用此属性设置选择文件对话框的
InitialDirectory
时,如下所示:

    private void AddFileDialog()
    {
        System.Windows.Forms.OpenFileDialog vfb = new System.Windows.Forms.OpenFileDialog();
        vfb.Multiselect = true;
        vfb.Title = "pouet";
        vfb.RestoreDirectory = true;
        vfb.InitialDirectory = DefaultFolder;
        System.Windows.Forms.DialogResult dr = vfb.ShowDialog();
        if (dr == System.Windows.Forms.DialogResult.OK)
        {
            this.Dispatcher.Invoke(DispatcherPriority.Send, new Action(delegate
            {
                for (var i = 0; i < vfb.FileNames.Length; i++)
                {
                    FileDisplay.Add(vfb.FileNames[i]);                    
                }
            }));
        }
    }

有什么想法吗?

你能发布你的异常的stacktrace吗,请?以下是我到目前为止得到的信息:WindowsBase.dll中发生了“System.InvalidOperationException”类型的未处理异常。其他信息:调用线程无法访问此对象,因为其他线程拥有它。因此@lecloneur您应该调用此代码
vfb.InitialDirectory=DefaultFolder(使用
调度程序
,就像您在代码的其他部分中所做的那样)
    private void AddFileDialog()
    {
        System.Windows.Forms.OpenFileDialog vfb = new System.Windows.Forms.OpenFileDialog();
        vfb.Multiselect = true;
        vfb.Title = "pouet";
        vfb.RestoreDirectory = true;
        vfb.InitialDirectory = DefaultFolder;
        System.Windows.Forms.DialogResult dr = vfb.ShowDialog();
        if (dr == System.Windows.Forms.DialogResult.OK)
        {
            this.Dispatcher.Invoke(DispatcherPriority.Send, new Action(delegate
            {
                for (var i = 0; i < vfb.FileNames.Length; i++)
                {
                    FileDisplay.Add(vfb.FileNames[i]);                    
                }
            }));
        }
    }
An unhandled exception of type 'System.InvalidOperationException' occurred in WindowsBase.dll Additional information: 
The calling thread cannot access this object because a different thread owns it.