5

我在 wpf 的 StatusBar 中有一个要更新的文本框。

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

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

4

2 回答 2

4

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

直接从代码中设置内容
您需要为其命名,TextBox以便您可以访问它的内容:

XAML

<TextBox x:Name="myTextBox" />

C#

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

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

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

有关数据绑定的更多信息,请参阅数据绑定概述

于 2010-04-30T10:23:17.320 回答
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}"/>
于 2010-04-30T10:07:14.013 回答