Note that there are some explanatory texts on larger screens.

plurals
  1. POCan't get TextBox to update OnPropertyChanged from string
    primarykey
    data
    text
    <p>After having used this site for a long time I finally decided a problem I keep encountering and that drives me crazy. I have been using a WPF TextBox to display progress for the program I'm writing, and as such I have bound it to a custom Log. However: the textbox does not update. What am I doing wrong?</p> <p>In the MainWindow.xaml.cs:</p> <pre><code>public MainWindow() { ... DataContext = this; ... } </code></pre> <p>In the MainWindow.</p> <pre><code>&lt;ScrollViewer Grid.Column="0" Grid.Row="2"&gt; &lt;TextBox Name="ErrorConsole" AcceptsReturn="true" TextWrapping="Wrap" Margin="0,3,10,0" TextChanged="Console_TextChanged" Background="Black" Foreground="White" /&gt; &lt;/ScrollViewer&gt; </code></pre> <p>And in the Log.cs</p> <pre><code>public class Log : INotifyPropertyChanged { private string _logText = ""; private static Log instance; public static string filename { get; set; } public event PropertyChangedEventHandler PropertyChanged; public static Log GetInstance() { if (instance == null) { instance = new Log(); } return instance; } public string logText { get { return _logText; } set { if (!(_logText == value)) { _logText = value; OnPropertyChanged(SystemStrings.status_Updated); } } } private void OnPropertyChanged(string propertyName) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) { handler(this, new PropertyChangedEventArgs(propertyName)); } } //Adds custom messages to the logfile public void Add(string message) { logText += (message + "\n"); logText += SystemStrings.divider + "\n\n"; } public void AddSimple(string message) { logText += (message + "\n"); } //Cleans the log that is in memory public void Clear() { logText = ""; } } </code></pre> <p>And the moment the user presses the "start" button, this code is executed:</p> <pre><code>Binding binding = new Binding(); binding.Source = Log.GetInstance(); binding.Mode = BindingMode.TwoWay; binding.Path = new PropertyPath(SystemStrings.status_Updated); BindingOperations.SetBinding(ErrorConsole, TextBox.TextProperty, binding); </code></pre> <p>I have tried the binding 1-way and 2-way, but no luck so far. However: if I put set it to 2way and use this code</p> <pre><code>private void Console_TextChanged(object sender, TextChangedEventArgs e) { ErrorConsole.Text = Log.GetInstance().logText; } </code></pre> <p>the textbox (ErrorConsole) will actually update to the proper text as soon as you type a single character.</p> <p>Any help here would be greatly appreciated because it's those little things that polish the program, which, despite not being the most graphically advanced one, should use at least somewhat nice in my opinion.</p> <p>-Peter</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload