在WPF中创建控制台输出窗口窗格
本文关键字:窗口 输出 控制台 WPF 创建 | 更新日期: 2023-09-27 18:14:04
在WPF中,我试图创建一个输出窗口。它应该是类似控制台输出的东西,它显示了一些函数的活动信息,如输出消息,函数调用的时间等,类似于vs的输出窗格。为此,我认为创建一个groupbox与textblock,然后更改其文本,如果需要:
<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>
在后面的代码中,我创建了一个函数来更改文本:
public void changeConsoleWindowText(string userProvidedString)
{
myConsoleWindowTextBlock.Text = userProvidedString;
}
现在,我可以改变其他类的文本:
MainWindow testConsole = new MainWindow();
testConsole.changeConsoleWindowText("This is a test to change the text.");
这里的问题是,每次我更改文本时,我都需要刷新MainWindow
来查看效果,根据这里的代码,这是合理的。但这不是我想要的,也没有效果。我需要它通过刷新MainWindow
的ConsoleWindow
成员来显示更改的新文本。此外,我试图避免任何多线程的方式来做这件事。我到处找了半天也找不到解决办法。你对此有什么建议吗?谢谢你!
将ViewModel类的字符串属性绑定到TextBox并填充此属性,而不是使用方法。这个字符串属性需要有INotifyPropertyChanged接口。