Display "current working directory" in WPF TextBox

Can someone tell me how to display the path to the current working directory in a text box using C # and WPF?

I do not understand how I can become attached to him.

+4
source share
3 answers
  • In the ViewModel / View code behind:

    public string CurrentDirectoryPath { get { return Environment.CurrentDirectory; } } 
  • In the XAML view:

     <TextBox Text="{Binding CurrentDirectoryPath}" /> 
  • Setting DataContext Rights

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

One solution is to create a property in a window (or another 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}" /> 
+2
source

you can also do something like

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

Source: https://habr.com/ru/post/1389112/


All Articles