Laufried Wiesewald
Laufried Wiesewald

Reputation: 43

Display "current working directory" in WPF TextBox

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

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

Upvotes: 4

Views: 4728

Answers (3)

MethodMan
MethodMan

Reputation: 18843

you could also do something like

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

Upvotes: 0

sll
sll

Reputation: 62504

  1. In ViewModel/View's code behind:

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

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

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

Upvotes: 9

Krzysztof
Krzysztof

Reputation: 16130

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:

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

Upvotes: 2

Related Questions