请问如何使用C#和WPF在文本框中显示当前工作目录的路径?
我不理解如何进行数据绑定。
请问如何使用C#和WPF在文本框中显示当前工作目录的路径?
我不理解如何进行数据绑定。
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() };
一种解决方案是在window(或其他父容器)中创建属性:
public string CurrentPath
{
get
{
return Environment.CurrentDirectory;
}
}
然后在 XAML 中按以下方式绑定:
<TextBox Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=CurrentPath, Mode=OneTime}" />
你也可以这样做
public string CurrentPath
{
get
{
return AppDomain.CurrentDomain.BaseDirectory;
}
}