在WPF TextBox中显示“当前工作目录”

时间:2021-06-06 23:06:58

Could someone please tell me how to display the path of the current working directory in a textbox using C# and WPF?

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

I don't understand how I can bind to it.

我不明白我怎么能绑定它。

3 个解决方案

#1


9  

  1. In ViewModel/View's code behind:

    在ViewModel / View的代码背后:

    public string CurrentDirectoryPath
    {
       get 
       { 
           return Environment.CurrentDirectory;
       }
    }
    
  2. In View's XAML:

    在View的XAML中:

    <TextBox Text="{Binding CurrentDirectoryPath}" />
    
  3. Setup right DataContext

    设置权限DataContext

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

#2


2  

One solution is to create property in window (or some other parent container):

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

public string CurrentPath
{
    get
    {
        return Environment.CurrentDirectory;
    }
}

And bind in XAML like this:

并在XAML中绑定如下:

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

#3


0  

you could also do something like

你也可以做点什么

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

#1


9  

  1. In ViewModel/View's code behind:

    在ViewModel / View的代码背后:

    public string CurrentDirectoryPath
    {
       get 
       { 
           return Environment.CurrentDirectory;
       }
    }
    
  2. In View's XAML:

    在View的XAML中:

    <TextBox Text="{Binding CurrentDirectoryPath}" />
    
  3. Setup right DataContext

    设置权限DataContext

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

#2


2  

One solution is to create property in window (or some other parent container):

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

public string CurrentPath
{
    get
    {
        return Environment.CurrentDirectory;
    }
}

And bind in XAML like this:

并在XAML中绑定如下:

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

#3


0  

you could also do something like

你也可以做点什么

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