Search code examples
c#wpfxamltemplates

XAML WPF Progressbar into ListBox width auto stretch with the window resize


I am using a progress bar to represents the transferred data in a WPF application.

A new progress bar (and a new item into the ListBox) will be instantiated dynamically for each transfer.

I have a XAML GUI problem: the size of the progres bar is fixed and doesn't stretch with the width of the window. Each elements of the windows reacts correctly at window resize except for the progress bar whose width still remains fixed.

How I can made my progress bar width automatically fitted to the ListBox?

Here is the portion of XAML code relative to the ListBox and the progress bar template:

    <ListBox Grid.Column="2" Name="TransfersList" Margin="0.111,0,-0.222,34.889" ItemsSource="{Binding DataTx}"
             SelectionChanged="TransfersList_SelectionChanged" Grid.Row="1" Grid.ColumnSpan="2">
        <ListBox.ContextMenu>
            <ContextMenu>
                <MenuItem Header="Ottieni informazioni" Click="GetInfo" />
                <MenuItem Header="Metti in pausa" Click="PauseTransfer" />
                <MenuItem Header="Riprendi trasferimento" Click="ResumeTransfer" />
                <MenuItem Header="Annulla trasferimento" Click="StopTransfer" />
            </ContextMenu>
        </ListBox.ContextMenu>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <ProgressBar Height="20" Minimum="0" Maximum="100" Name="gasparino_il_carbonaro"
                             Value="{Binding PbStatus}" Width="177" />
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

enter image description here

enter image description here

I tried also with (on the ProgressBar template):

  • HorizontalAlignment="Stretch"
  • HorizontalContentAlignment="Stretch"

but with the same following result:

enter image description here

Note that the progress bar item results correctly stretched, but the bar (grey and gree visible part) results compressed (see red arrow).


Solution

  • Setting HorizontalContentAlignment="Stretch" on the ListBox should work.