Msdnexpert
Msdnexpert

Reputation: 284

Updating progress in TextBlock using background thread

My WPF Window has a TextBlock encased in a ScrollViewer.

<Window x:Class="WpfScrollProgress.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="545" Width="662"
    Loaded="OnLoad">
    <Grid>
        <Label Content="Please wait while the items are being processed" Height="28" HorizontalAlignment="Left" Margin="12,12,0,0" Name="lblStatus" VerticalAlignment="Top" Width="263" />
        <ScrollViewer Name="ScrollViewer1" Margin="10,46,0,136">
            <TextBlock Height="293" HorizontalAlignment="Left" Margin="10,15,10,100" Name="txtStatus" Text="" VerticalAlignment="Top" Width="574" Canvas.Left="-9" />
        </ScrollViewer>
    </Grid>
</Window>

I am trying to update the progress status using a background thread. However the scroll action doesnt seem to work when the status is updated in the text block

using System;
using System.Windows;
using System.Threading.Tasks;
using System.Threading;

namespace WpfScrollProgress
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        private void OnLoad(System.Object sender, System.Windows.RoutedEventArgs e)
        {
            Action<object> oProgressAction = (object s) => { RefreshProgressStatus((string)s); };
            ProgressReporter oProgressReporter = new ProgressReporter(TaskScheduler.FromCurrentSynchronizationContext(), oProgressAction);

            Task.Factory.StartNew(() =>
            {
                for (int i = 0; i <= 100; i++)
                {
                    oProgressReporter.ReportProgress("Hello " + i);
                    Thread.Sleep(100);
                }
            });
        }

        public void RefreshProgressStatus(string strUpdate)
        {
            txtStatus.Text = txtStatus.Text  + strUpdate + "............" + Environment.NewLine;
        }

    }

    public class ProgressReporter
    {

        private TaskScheduler _oScheduler;
        private string _strStatus;

        private Action<object> _oProgressAction;
        public ProgressReporter(TaskScheduler oScheduler, Action<object> oProgressAction)
        {
            _oScheduler = oScheduler;
            _oProgressAction = oProgressAction;
        }

        public TaskScheduler Scheduler
        {
            get { return _oScheduler; }
        }

        private Task ReportProgressAsync(Action<object> action)
        {
            return Task.Factory.StartNew(action, _strStatus, CancellationToken.None, TaskCreationOptions.None, this.Scheduler);
        }

        public void ReportProgress(string strStatus)
        {
            _strStatus = strStatus + "..........." ;
            this.ReportProgressAsync(_oProgressAction).Wait();
        }

    }

}

The scrolling action doesnt seem to work on the ScrollViewer and the TextBlock contents are not displayed beyond the ScrollViewer. Is something missing out here?

Upvotes: 1

Views: 251

Answers (1)

Fede
Fede

Reputation: 44068

Your TextBlock has a fixed Width and Height. It will NOT stretch/grow beyond that. If your text occupies more space than the Textblock's Width, it will get cropped.

Remove the Height and Width properties.

I suggest you read up on basic WPF layout/design concepts, and also how to properly update UI elements properties via DataBinding, as opposed to using procedural code.

Upvotes: 1

Related Questions