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

5

我在wpf的StatusBar中有一个TextBox,我想要更新它。

我有一个ListBox中的文件列表。对于每个文件,我将调用一个方法ProcessFile()进行某些操作。因此,每当文件处理完成后,我都希望在StatusBar文本中显示该文件的名称。

我尝试过类似这样的代码:

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;
    }

但是我只能在状态栏中看到最后一个文件的名称。代码出了什么问题?我该如何纠正它?

2个回答

4

有更多的方法来实现这个。

直接从代码中设置内容
你需要给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));
        }
    }
} 

了解更多数据绑定的信息,请参见数据绑定概述


为什么要创建一个单独的类,如果只需要使用几行代码就可以使用Dispatcher完成呢? - Archie
请参阅文章“数据绑定概述”以获取说明。链接在答案末尾上方。 - zendar

1

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

每次处理文件时,我会将属性“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}"/>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接