Refresh text in StatusBar in wpf using c #

I have a TextBox in StatusBar in wpf that I want to update.

I have a list of files in a ListBox. On each file, I would perform some operation by calling the ProcessFile () method. So whenever the file processing is done, I want to show this filename in the StatusBar text.

I've 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 filename in the StatusBar. What's wrong with the code? Can someone fix this? Thanks.

+2


a source to share


2 answers


There are more ways to do this.

Install content directly from code
You need to provide a name TextBox

so you can access it:

XAML

<TextBox x:Name="myTextBox" />

      

FROM#

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

      



Use data binding
You need to create some kind of object and set it as DataContext

in TextBox

or some WPF element that contains this text field (status bar, window, ...).

XAML:

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

      

FROM#

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

+2


a source


When using the ViewModel, I would define a "ProcessedFile" property in your ViewModel and bind your StatusBar's textbox to the property.

Every time you process a file, I would set the "ProcessedFile" property to the filename.

Here is the 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
    }
}

      

Here the XAML binds the TextBox to a property. (I am assuming the ViewModel is set as DataContext for the TextBox)

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

      

0


a source







All Articles