使用 C# 更新 wpf 中 StatusBar 中的文本

发布于 2024-08-31 01:17:25 字数 1044 浏览 4 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

2024-09-07 01:17:25

还有更多方法可以做到这一点。

直接从代码设置内容
您需要为 TextBox 指定名称,以便可以访问其内容:

XAML

<TextBox x:Name="myTextBox" />

C#

...
ProcessFile(someFileName);
myTextBox.Text = someFileName;

使用数据绑定
您需要创建一些对象并将其设置为 TextBox 或包含该文本框(状态栏、窗口等)的某个 WPF 元素的 DataContext

XAML:

<TextBox Text="{Binding Path=ProcessedFileName}" />

C#

public MyClass : INotifyPropertyChanged
{
    public string ProcessedFileName {get; set;} 

    public void ProcessFile(string someFileName)
    {
       // Processing file code here

       // When done processing, set file name to property
       ProcessedFileName = someFileName;
       OnPropertyChanged("ProcessedFileName");
    } 

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;

        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
} 

有关数据绑定的详细信息,请参阅数据绑定概述

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

<TextBox x:Name="myTextBox" />

C#

...
ProcessFile(someFileName);
myTextBox.Text = someFileName;

Use data binding
You need to create some object and set it as DataContext to the TextBox or some WPF element that contain that text box (status bar, window, ...).

XAML:

<TextBox Text="{Binding Path=ProcessedFileName}" />

C#

public MyClass : INotifyPropertyChanged
{
    public string ProcessedFileName {get; set;} 

    public void ProcessFile(string someFileName)
    {
       // Processing file code here

       // When done processing, set file name to property
       ProcessedFileName = someFileName;
       OnPropertyChanged("ProcessedFileName");
    } 

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;

        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
} 

For more information on data binding see Data Binding Overview

将军与妓 2024-09-07 01:17:25

当您使用 ViewModel 时,我会在 ViewModel 中定义一个属性“ProcessedFile”,并将 StatusBar 的文本框绑定到该属性。

每次处理文件时,我都会将属性“ProcessedFile”设置为文件名。

这是 ViewModel 的一些代码。

public class ViewModel : INotifyPropertyChanged {
    private string _processedFile;
    public string ProcessedFile {
        get {
            return _processedFile;
        }
        set {

            if (_processedFile != value) {
                _processedFile = value;

                if (PropertyChanged != null) {
                    PropertyChanged(this, new PropertyChangedEventArgs("ProcessedFile"));
                }
            }
        }
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    #endregion

    public void ProcessFile() {
       // Process the file
       ProcessedFile = //Set the Property to the processed file
    }
}

这是将 TextBox 绑定到属性的 XAML。 (我假设 ViewModel 设置为 TextBox 的 DataContext)

<TextBox Text="{Binding ProcessedFile, Mode=OneWay}"/>

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.

public class ViewModel : INotifyPropertyChanged {
    private string _processedFile;
    public string ProcessedFile {
        get {
            return _processedFile;
        }
        set {

            if (_processedFile != value) {
                _processedFile = value;

                if (PropertyChanged != null) {
                    PropertyChanged(this, new PropertyChangedEventArgs("ProcessedFile"));
                }
            }
        }
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    #endregion

    public void ProcessFile() {
       // Process the file
       ProcessedFile = //Set the Property to the processed file
    }
}

Heres the XAML to bind the TextBox to the Property. (I assume that the ViewModel is set as DataContext for the TextBox)

<TextBox Text="{Binding ProcessedFile, Mode=OneWay}"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文