Reputation: 43
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
Reputation: 18843
you could also do something like
public string CurrentPath
{
get
{
return AppDomain.CurrentDomain.BaseDirectory;
}
}
Upvotes: 0
Reputation: 62504
In ViewModel/View's code behind:
public string CurrentDirectoryPath
{
get
{
return Environment.CurrentDirectory;
}
}
In View's XAML:
<TextBox Text="{Binding CurrentDirectoryPath}" />
Setup right DataContext
// If you are using MVVM:
var view = new MyView { DataContext = new MyViewModel() };
Upvotes: 9
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