使用 C# 更新 wpf 中 StatusBar 中的文本
我在 wpf 的 StatusBar 中有一个 TextBox,我想更新它。
我在 ListBox 中有一个文件列表。在每个文件上,我将通过调用 ProcessFile() 方法来执行一些操作。因此,每当文件处理完成时,我想在状态栏文本中显示该文件的名称。
我尝试过这样的操作:
private void button_Click(object sender, RoutedEventArgs e)
{
statusBar.Visibility = Visibility.Visible;
DispatcherFrame frame = new DispatcherFrame();
Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, new DispatcherOperationCallback(TimeConsumingMethod), frame);
Dispatcher.PushFrame(frame);
statusBar.Visibility = Visibility.Collapsed;
}
public object TimeConsumingMethod(Object arg)
{
((DispatcherFrame)arg).Continue = false;
foreach (string fileName in destinationFilesList.Items)
{
txtStatus.Text = fileName.ToString();
//Assume that each process takes some time to complete
System.Threading.Thread.Sleep(1000);
}
return null;
}
但我只能在状态栏中看到最后一个文件的名称。代码有什么问题吗?我怎样才能纠正它?
I have a TextBox in StatusBar in wpf which I want to update.
I have a list of files in ListBox. On each file I would be doing some operation by calling say method ProcessFile(). So whenever the file processing is completed I want to show that file's name in the StatusBar text.
I have tried something like this:
private void button_Click(object sender, RoutedEventArgs e)
{
statusBar.Visibility = Visibility.Visible;
DispatcherFrame frame = new DispatcherFrame();
Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, new DispatcherOperationCallback(TimeConsumingMethod), frame);
Dispatcher.PushFrame(frame);
statusBar.Visibility = Visibility.Collapsed;
}
public object TimeConsumingMethod(Object arg)
{
((DispatcherFrame)arg).Continue = false;
foreach (string fileName in destinationFilesList.Items)
{
txtStatus.Text = fileName.ToString();
//Assume that each process takes some time to complete
System.Threading.Thread.Sleep(1000);
}
return null;
}
But I can only see the last file's name in the StatusBar. What's wrong with the code? How can I correct it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
还有更多方法可以做到这一点。
直接从代码设置内容
您需要为
TextBox
指定名称,以便可以访问其内容:XAML
C#
使用数据绑定
您需要创建一些对象并将其设置为
TextBox
或包含该文本框(状态栏、窗口等)的某个 WPF 元素的DataContext
。XAML:
C#
有关数据绑定的详细信息,请参阅数据绑定概述
There's more ways to do this.
Set content directly from code
You need give name to the
TextBox
so that you can access it's content:XAML
C#
Use data binding
You need to create some object and set it as
DataContext
to theTextBox
or some WPF element that contain that text box (status bar, window, ...).XAML:
C#
For more information on data binding see Data Binding Overview
当您使用 ViewModel 时,我会在 ViewModel 中定义一个属性“ProcessedFile”,并将 StatusBar 的文本框绑定到该属性。
每次处理文件时,我都会将属性“ProcessedFile”设置为文件名。
这是 ViewModel 的一些代码。
这是将 TextBox 绑定到属性的 XAML。 (我假设 ViewModel 设置为 TextBox 的 DataContext)
When you are using a ViewModel, i would define a Property "ProcessedFile" in your ViewModel and bind the Textbox of your StatusBar to the Property.
Every time you processed a file i would set the Property "ProcessedFile" to the name of the file.
Here´s some code for the ViewModel.
Heres the XAML to bind the TextBox to the Property. (I assume that the ViewModel is set as DataContext for the TextBox)