E_learner
E_learner

Reputation: 3582

Create console output window pane in WPF

In WPF, I am trying to create an output window. It should be something like console output, which shows information of some functions' activity like output message, time of the function call, etc, similar to output pane of VS. For that, I think of creating a groupbox with textblock then change its text if needed:

<Windows x:Class="MyProject.MainWindow" ... >
    <Grid Margin="0">

        <!-- Some other UI member may come here -->

        <GroupBox Header="Console" Grid.Column="1" Margin="0,590,0,0" HorizontalAlignment="Stretch" x:Name="ConsoleWindow" IsEnabled="True" VerticalAlignment="Stretch"
                  >
            <TextBlock x:Name="myConsoleWindowTextBlock"/>
        </GroupBox>

    </Grid>
</Window>

In the code behind, I created a function to change the text:

    public void changeConsoleWindowText(string userProvidedString)
    {
        myConsoleWindowTextBlock.Text = userProvidedString;            
    }

Now, I can change the text in other class by:

MainWindow testConsole = new MainWindow();
testConsole.changeConsoleWindowText("This is a test to change the text.");

The problem here is, each and every time I change the text, I need to refresh the MainWindow to see the effect, which is reasonable according to the code here. But this is not what I want, and not effective. I need it to display the changed new text by just refreshing the ConsoleWindow member of the MainWindow. Also, I try to escape any multi-threaded way of doing it. I couldn't figure out any solution after searching around a lot. Could you have any suggestion for this? Thank you.

Upvotes: 0

Views: 992

Answers (1)

iop
iop

Reputation: 312

Bind a string property of your ViewModel class to your TextBox and fill this instead of using a method. This string property needs to have the INotifyPropertyChanged interface.

Upvotes: 1

Related Questions