0

Ive got a simple StackPanel which contains a TextBlock and ProgressBar.

On startup, the width of the ProgressBar is perfect - it has sized itself to fit in its boundaries (stretched to '150' by it's sibling TextBlock).

However, when the TextBlock has stretched to a width further than 150 due to additional Text (which the ProgressBar nicely expands to fill the extra area), and is then shrunk, due to the removal of it's Text, the Width of the progress bar remains.

This only happens when IsIndeterminate = true

enter image description here

The following hastily put together code will show the workings;

<Grid HorizontalAlignment="Center" VerticalAlignment="Center">
    <StackPanel>
        <TextBlock MinWidth="150" Name="tb"  HorizontalAlignment="Left"></TextBlock>
       <ProgressBar MinHeight="20" IsIndeterminate="True"/>
    </StackPanel>
</Grid>


public MainWindow()
{
    InitializeComponent();
    new Thread(() =>
    {
        Thread.Sleep(2000);
        App.Current.Dispatcher.Invoke(() =>
        {
            tb.Text = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
        });
        Thread.Sleep(1000);
        App.Current.Dispatcher.Invoke(() =>
        {
            tb.Text = "A";
        });
    }).Start();
}
maxp
  • 24,209
  • 39
  • 123
  • 201

1 Answers1

2

You could bind the Width of the Progressbar to the ActualWidth of the TextBlock.

<TextBlock MinWidth="150" Name="tb"  HorizontalAlignment="Left" />
<ProgressBar MinHeight="20" IsIndeterminate="True" 
    Width="{Binding Path=ActualWidth, ElementName=tb, Mode=OneWay}"/>
Frank J
  • 1,666
  • 13
  • 19