0

I have a textbox which I need to bind a string to.

<TextBox Name="txtDoc" Margin="5" Text ="{Binding Source={x:Static local:DocumentViewModel.FileText}, Path=FileText}">

The FileText property is changed on a different class:

DocumentViewModel.GetInstance().FileText = File.ReadAllText(document.Path);

The DocumentViewModel is a class with 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;
    }
}

I need to be able to change the value of the FileText property and reflect this change in the textbox. It's not working. I tried using TextBox as a static property but then the Onp

1 Answer 1

2

Try to set the source to your viewmodel instead of the property itself, and set the instance property to public? {Binding Source={x:Static local:DocumentViewModel.instance}, Path=FileText}

Edit: Included a complete example, that working for me:

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>

Code-behind:

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
}
Sign up to request clarification or add additional context in comments.

6 Comments

"I get an error: 'local:DocumentViewModel' member is not valid because it does not have a qualifying type name.
@Gil: The instance needs to be exposed as a public property, right now it's a private field.
Not working... I get no errors but the textbox doesn't show the text.
I tried testing the binding: I assigned a string value directly to the FileText : FileText = "Gil" and it did appear on the text box. Then I tried assigning the FileText property from another class: DocumentViewModel.FileText = "Gil"; this one didn't change the textbox. so the problem is not with the binding, it's with teh updating.
@Gil Included a complete example, that working for me. I changed your class to my taste, but the essence is the same.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.