Frage

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?

In the MainWindow.xaml.cs:

public MainWindow()
{
    ...
    DataContext = this;
    ...
}

In the MainWindow.

<ScrollViewer Grid.Column="0" Grid.Row="2">
    <TextBox Name="ErrorConsole" AcceptsReturn="true" TextWrapping="Wrap"  Margin="0,3,10,0" TextChanged="Console_TextChanged" Background="Black" Foreground="White" />
</ScrollViewer>

And in the Log.cs

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 = "";
    }
}

And the moment the user presses the "start" button, this code is executed:

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);

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

private void Console_TextChanged(object sender, TextChangedEventArgs e)
{
    ErrorConsole.Text = Log.GetInstance().logText;
}

the textbox (ErrorConsole) will actually update to the proper text as soon as you type a single character.

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.

-Peter

War es hilfreich?

Lösung

Turns out there is no proper way to do it, but I managed to solve the issue by playing around a bit with a (not so neatly used) Observable Collection. Thanks for the help everyone.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top