微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

WPF绑定问题

如何解决WPF绑定问题

| 我有一个文本框,需要将字符串绑定到该文本框。
<TextBox Name=\"txtDoc\" Margin=\"5\" Text =\"{Binding Source={x:Static local:Documentviewmodel.FileText},Path=FileText}\">
FileText属性在另一个类上更改:
Documentviewmodel.GetInstance().FileText = File.ReadAllText(document.Path);
Documentviewmodel是具有Singleton的类:
public class Documentviewmodel : INotifyPropertyChanged
{
    private static string fileText;

    public string FileText
    {
        get { return fileText; }
        set
        {
            fileText = value; // Call OnPropertyChanged whenever the property is updated
            OnPropertyChanged(\"FileText\");
        }
    }

   private void OnPropertyChanged(string filetext)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this,new PropertyChangedEventArgs(filetext));
        }
    }


    public event PropertyChangedEventHandler PropertyChanged = delegate { };

    private static Documentviewmodel instance = new Documentviewmodel();

    private Documentviewmodel() { }

    public static Documentviewmodel GetInstance()
    {
        return instance;
    }
}
我需要能够更改FileText属性的值,并在文本框中反映此更改。 它不起作用。 我尝试将TextBox用作静态属性,但随后将Onp     

解决方法

        尝试将源设置为视图模型,而不是属性本身,并将and3ѭ属性设置为public?
{Binding Source={x:Static local:DocumentViewModel.instance},Path=FileText}
编辑:包括一个完整的示例,为我工作: Xaml:
<Window x:Class=\"Test.MainWindow\"
    xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"
    xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\"
    xmlns:local=\"clr-namespace:Test\"
    Title=\"MainWindow\" Height=\"350\" Width=\"525\"
    Loaded=\"Window_Loaded\">
  <TextBox Name=\"txtDoc\" Margin=\"5\"
           Text=\"{Binding Source={x:Static local:DocumentViewModel.Instance},Path=FileText}\" />
</Window>
后台代码:
public partial class MainWindow : Window
{
  public MainWindow()
  {
    InitializeComponent();
  }

  private void Window_Loaded(object sender,RoutedEventArgs e)
  {
    DocumentViewModel.Instance.FileText = \"Hello world!\";      
  }
}

public class DocumentViewModel : INotifyPropertyChanged
{
  #region Singleton implementation

  // Static constructor to create the singleton instance.
  static DocumentViewModel()
  {
    DocumentViewModel.Instance = new DocumentViewModel();
  }

  public static DocumentViewModel Instance { get; private set; }

  #endregion

  private static string fileText;
  public string FileText
  {
    get { return fileText; }
    set
    {
      if (fileText != value)
      {
        fileText = value;
        OnPropertyChanged(\"FileText\");
      }
    }
  }

  #region INotifyPropertyChanged

  private void OnPropertyChanged(string filetext)
  {
    PropertyChangedEventHandler handler = PropertyChanged;
    if (handler != null)
    {
      handler(this,new PropertyChangedEventArgs(filetext));
    }
  }

  public event PropertyChangedEventHandler PropertyChanged;

  #endregion
}
    

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。