C# “显示”;“当前工作目录”;在WPF文本框中

C# “显示”;“当前工作目录”;在WPF文本框中,c#,wpf,binding,textbox,C#,Wpf,Binding,Textbox,有人能告诉我如何使用C#和WPF在文本框中显示当前工作目录的路径吗 我不明白如何绑定到它。一个解决方案是在窗口(或其他父容器)中创建属性: 然后像这样绑定到XAML中: <TextBox Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=CurrentPath, Mode=OneTime}" /> 在ViewModel/View的代码隐藏

有人能告诉我如何使用C#和WPF在文本框中显示当前工作目录的路径吗


我不明白如何绑定到它。

一个解决方案是在窗口(或其他父容器)中创建属性:

然后像这样绑定到XAML中:

<TextBox Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=CurrentPath, Mode=OneTime}" />

  • 在ViewModel/View的代码隐藏中:

    public string CurrentDirectoryPath
    {
       get 
       { 
           return Environment.CurrentDirectory;
       }
    }
    
  • 在视图的XAML中:

    
    
  • 设置右数据上下文

    // If you are using MVVM:
    var view = new MyView { DataContext = new MyViewModel() };
    

  • 你也可以这样做

    public string CurrentPath 
    {     
        get     
        { 
            return AppDomain.CurrentDomain.BaseDirectory;     
        } 
    } 
    

    绑定
    需要大量代码。在我们帮助您之前,您需要提供一个您已经做过的事情的示例。您要绑定到的是什么:目录还是文本框。我知道你不熟悉SO,但这个问题不是很清楚。你能试试这个sPath=System.AppDomain.CurrentDomain.BaseDirectory;SAPPATH=Environment.CurrentDirectory;我给a+1只是为了理解这个问题。谢谢你快速而有用的回答!
    public string CurrentPath 
    {     
        get     
        { 
            return AppDomain.CurrentDomain.BaseDirectory;     
        } 
    }