Problems:
How to add progress bar into list box as shown image / custom progress bar ?
The progress bar value increment represent the time elapsed (sec).
![]()
MainWindow.xaml
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:wpf_ItemsControl_Wrapped"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
x:Class="wpf_ItemsControl_Wrapped.MainWindow"
mc:Ignorable="d" Title="MainWindow" Height="350" Width="525"><Grid><Grid.Resources><Style TargetType="{x:Type Button}"><Setter Property="Margin" Value="4"/></Style></Grid.Resources><Grid.RowDefinitions><RowDefinition Height="Auto" /><RowDefinition Height="Auto" /><RowDefinition Height="Auto" /><RowDefinition Height="*"/></Grid.RowDefinitions><Button HorizontalAlignment="Center" Content="Start" Command="{Binding StartCommand}" Width="80" Height="40" /><StackPanel Orientation="Horizontal" Grid.Row="1" Margin="4, 8, 4, 8"><TextBlock Text="Current Value:" VerticalAlignment="Center" /><!--TBC--><!--<TextBlock Text="x" VerticalAlignment="Center" />--></StackPanel><StackPanel Orientation="Horizontal" Grid.Row="2" Margin="4, 8, 4, 8"><TextBlock Text="Time Elapsed: " VerticalAlignment="Center" /><!--TBC--><!--<TextBlock Text="x" VerticalAlignment="Center" />--></StackPanel><ListBox Grid.Row="3" Grid.IsSharedSizeScope="True" AlternationCount="10000" ItemsSource="{Binding Models}" ScrollViewer.HorizontalScrollBarVisibility="Disabled"><ListBox.ItemContainerStyle><Style TargetType="{x:Type ListBoxItem}"><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="{x:Type ListBoxItem}"><ContentPresenter/></ControlTemplate></Setter.Value></Setter></Style></ListBox.ItemContainerStyle><ListBox.ItemsPanel><ItemsPanelTemplate><WrapPanel Orientation="Horizontal" Margin="15, 0, 15, 0" /></ItemsPanelTemplate></ListBox.ItemsPanel><ListBox.ItemTemplate><DataTemplate><Grid><Grid.RowDefinitions><RowDefinition Height="Auto" /><RowDefinition Height="30" /></Grid.RowDefinitions><Grid.ColumnDefinitions><ColumnDefinition MinWidth="120" SharedSizeGroup="AllSameWidth" /></Grid.ColumnDefinitions><Grid><local:ItemTracer HorizontalAlignment="Stretch" Container="{Binding ., RelativeSource={RelativeSource AncestorType={x:Type WrapPanel}}}" ContainerWidth="{Binding ActualWidth, RelativeSource={RelativeSource AncestorType={x:Type WrapPanel}}}" ItemIndex="{Binding (ItemsControl.AlternationIndex), RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}" ItemsCount="{Binding Items.Count, RelativeSource={RelativeSource AncestorType={x:Type ListBox}}}" x:Name="lastInLine" /><Border BorderThickness="1" BorderBrush="Gray" x:Name="border" Height="30"><Border.Style><Style TargetType="{x:Type Border}"><Style.Triggers><DataTrigger Binding="{Binding (ItemsControl.AlternationIndex), RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}" Value="0"><Setter Property="CornerRadius" Value="10, 0, 0, 10" /></DataTrigger><DataTrigger Binding="{Binding IsLastItem, ElementName=lastInLine}"
Value="true" ><Setter Property="CornerRadius" Value="0, 10, 10, 0" /></DataTrigger><DataTrigger Binding="{Binding Items.Count, RelativeSource={RelativeSource AncestorType={x:Type ListBox}}}" Value="1"><Setter Property="CornerRadius" Value="10, 10, 10, 10" /></DataTrigger></Style.Triggers></Style></Border.Style><TextBlock Text="{Binding CurrentValue}" VerticalAlignment="Center" HorizontalAlignment="Center" /></Border></Grid><TextBlock Text="{Binding StartTime, StringFormat=\{0:mm\\:ss\}}" Grid.Row="1" Margin="-14, 0, 0, 0" /><TextBlock Text="{Binding EndTime, StringFormat=\{0:mm\\:ss\}}" Grid.Row="1" HorizontalAlignment="Right"><TextBlock.RenderTransform><TranslateTransform X="14" Y="0" /></TextBlock.RenderTransform><TextBlock.Style><Style TargetType="{x:Type TextBlock}"><Setter Property="Visibility" Value="Visible"/><Style.Triggers><MultiDataTrigger><MultiDataTrigger.Conditions><Condition Binding="{Binding IsLastInLine, ElementName=lastInLine}" Value="False" /><Condition Binding="{Binding IsLastItem, ElementName=lastInLine}" Value="False" /></MultiDataTrigger.Conditions><Setter Property="Visibility" Value="Collapsed" /></MultiDataTrigger></Style.Triggers></Style></TextBlock.Style></TextBlock></Grid></DataTemplate></ListBox.ItemTemplate></ListBox></Grid></Window>
MainWindowViewModel.cs
using System;
using System.Collections.ObjectModel;
using System.Linq;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.CommandWpf;
namespace wpf_ItemsControl_Wrapped
{
public class MainWindowViewModel : ViewModelBase
{
private ObservableCollection<Model> models;
public ObservableCollection<Model> Models
{
get
{
if (models == null)
{
models = new ObservableCollection<Model>();
}
return models;
}
}
private ObservableCollection<Duration> durations;
public ObservableCollection<Duration> Durations
{
get
{
if (durations == null)
{
durations = new ObservableCollection<Duration>();
}
return durations;
}
}
private RelayCommand _startCommand;
public RelayCommand StartCommand
{
get
{
return _startCommand ?? (_startCommand = new RelayCommand(() =>
{
//tbc
//increase progress bar value to indicate time elapsed (use timer to simulate)
// get current value text from the border (based on current elapsed time)
}));
}
}
private TimeSpan stepTime = TimeSpan.FromSeconds(15);
public TimeSpan StepTime
{
get { return stepTime; }
set
{
stepTime = value;
RaisePropertyChanged();
}
}
public MainWindowViewModel()
{
Models.Add(new Model { StartTime = LastEnd(), Duration = TimeSpan.FromSeconds(10), CurrentValue = new Random().Next() });
Models.Add(new Model { StartTime = LastEnd(), Duration = TimeSpan.FromSeconds(20), CurrentValue = new Random().Next() });
Models.Add(new Model { StartTime = LastEnd(), Duration = TimeSpan.FromSeconds(30), CurrentValue = new Random().Next() });
Models.Add(new Model { StartTime = LastEnd(), Duration = TimeSpan.FromSeconds(40), CurrentValue = new Random().Next() });
Models.Add(new Model { StartTime = LastEnd(), Duration = TimeSpan.FromSeconds(50), CurrentValue = new Random().Next() });
Models.Add(new Model { StartTime = LastEnd(), Duration = TimeSpan.FromSeconds(60), CurrentValue = new Random().Next() });
}
public TimeSpan LastEnd()
{
return (Models.Count == 0) ? TimeSpan.FromSeconds(0) : Models.Last<Model>().EndTime;
}
}
}
Download Project (Credited to Andy ONeill)