【问题标题】:Display "current working directory" in WPF TextBox在 WPF 文本框中显示“当前工作目录”
【发布时间】:2012-02-01 17:06:12
【问题描述】:

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

我不明白如何绑定它。

【问题讨论】:

  • binding 需要大量代码。您需要提供一个您已经做过的某事的示例,然后我们才能为您提供帮助。
  • 你想绑定什么:目录或文本框。我知道您是 SO 新手,但这个问题不是很清楚。
  • 你能试试这个 sPath = System.AppDomain.CurrentDomain.BaseDirectory;和 sAppPath = Environment.CurrentDirectory;

标签: c# wpf binding textbox


【解决方案1】:
  1. 在 ViewModel/View 的代码后面:

    public string CurrentDirectoryPath
    {
       get 
       { 
           return Environment.CurrentDirectory;
       }
    }
    
  2. 在 View 的 XAML 中:

    <TextBox Text="{Binding CurrentDirectoryPath}" />
    
  3. 设置正确的DataContext

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

【讨论】:

  • 我给 +1 只是为了理解这个问题。
  • 感谢您快速而有帮助的回答!
【解决方案2】:

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

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

并像这样在 XAML 中绑定:

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

【讨论】:

    【解决方案3】:

    你也可以这样做

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-22
      • 2011-07-20
      相关资源
      最近更新 更多