Friedrich Rehren
Friedrich Rehren

Reputation: 41

Update TextBox content in WPF Window

I'm having a WPF Project, in which I'm performing an installation...

To show the installations progress, the window is containing a TextBox which I want to update using for example:

LogDisplay.AppendText("Initialising installation..." + "\r\n");

This works kind of...

The problem is that the content of the TextBox is only getting displayed when the installation is finished.

I have tried now several soluions, such as:

/*
LogDisplay.Update;
this.Update();
this.UpdateContent();
*/

But non of this was working for me...

The XAML code is:

<Window x:Class="Steam_Server_Installer.UI.ServerInstallation"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Steam_Server_Installer.UI"
        mc:Ignorable="d"
        Title="" Height="600" Width="900"
        WindowStartupLocation="CenterScreen">

    <Grid>
        <TextBox x:Name="LogDisplay" HorizontalAlignment="Left" VerticalAlignment="Top" Height="470" Width="650" Margin="30,90,0,0" IsReadOnly="True"/>
        <Button x:Name="cancel_button" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,530,115,0" Width="70" Content="Cancel" FontSize="16" Click="cancel_button_Click"/>
        <Button x:Name="finish_start_button" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,530,25,0" Width="70" Content="Finish" FontSize="16" Click="finish_start_button_Click" IsEnabled="False"/>
    </Grid> 
</Window>

If someone could tell me a working solution or point me to another question, which already discusses this question, it would be very much appreciated.

Upvotes: 1

Views: 4755

Answers (2)

Moslem Shahsavan
Moslem Shahsavan

Reputation: 1337

When use long task in UI thread, UI thread is not idle to update other content control. You must use create other thread and handle task to thread then update the UI control by using the UI thread.

1.Create Thread or Task
2.Work task ...
3.Update the UI thread with Application.Current.Dispatcher.Invoke(() => { TextBox.Text = "text"; });
4.Finish

Upvotes: 2

Ali Baig
Ali Baig

Reputation: 3867

Try using TextBlock rather than TextBox like this

<TextBlock x:Name="LogDisplay" HorizontalAlignment="Left" VerticalAlignment="Top" Height="470" Width="650" Margin="30,90,0,0" />

It would be better to use binding instead of setting it like this. First you will implement INotifyPropertyChanged in your .xaml.cs file like

public class YourClassName: Window, INotifyPropertyChanged
{
   public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

   //then create a string variable in your .xaml.cs file like   

    private string _logText;

    public string LogText
    {
         get{ return _logText;}
         set { _logText = value; OnPropertyChanged("LogText"); }
    }

    public YourClassName()
    {
         InitializeComponent();
         //setting data context of the window
         this.DataContext = this;
    }
}

And in your XAML, use:

<TextBlock Text="{Binding LogText, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" VerticalAlignment="Top" Height="470" Width="650" Margin="30,90,0,0" />

Now, you can just update LogText variable inside your class like

this.LogText = this.LogText + "Initialising installation..." + "\r\n";  //or better use StringBuilder and Append function

Upvotes: 2

Related Questions