I want to make a PartialProgressBar for a multithreading downloader but I couldn't make it to work. No change when I add element into the ItemsSource. How can I fix it?
MainWindow.
public partial class MainWindow : Window
{
ObservableCollection<HttpRange> Ranges;
public MainWindow()
{
InitializeComponent();
DataContext = this;
Ranges = new ObservableCollection<HttpRange>();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
var range1 = new HttpRange(start: 0, end: 50, saveDir: "path", fileId: "1");
var range2 = new HttpRange(start: 50, end: 100, saveDir: "path", fileId: "1");
Ranges.Add(range1);
Ranges.Add(range2);
}
}
MainWindow.xaml
<local:PartialProgressBar Background="Yellow"
HorizontalAlignment="Left"
VerticalAlignment="Top"
x:Name="prbar" Width="250" Height="20"
ContentSize="100"
Ranges="{Binding Ranges}" />
PartialProgressBar.xaml
<UserControl x:Class="Alto_Download_Manager.PartialProgressBar"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Alto_Download_Manager"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
<local:RangeToTotalWidthConverter x:Key="RangeToTotalWidthConverter"/>
<local:RangeToProgressConverter x:Key="RangeToProgressConverter"/>
</UserControl.Resources>
<!---->
<ItemsControl x:Name="list" ItemsSource="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}, Path=Ranges}">
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"></StackPanel>
</ItemsPanelTemplate>
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock>a</TextBlock>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
PartialProgressBar.xaml.cs
public partial class PartialProgressBar : UserControl
{
public PartialProgressBar()
{
InitializeComponent();
}
public long ContentSize
{
get { return (long)GetValue(ContentSizeProperty); }
set { SetValue(ContentSizeProperty, value); }
}
// Using a DependencyProperty as the backing store for ContentSize. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ContentSizeProperty =
DependencyProperty.Register("ContentSize", typeof(long), typeof(PartialProgressBar), new PropertyMetadata(1L));
public List<HttpRange> Ranges
{
get { return (List<HttpRange>)GetValue(RangesProperty); }
set
{
SetValue(RangesProperty, value);
}
}
// Using a DependencyProperty as the backing store for Ranges. This enables animation, styling, binding, etc...
public static readonly DependencyProperty RangesProperty =
DependencyProperty.Register("Ranges", typeof(List<HttpRange>), typeof(PartialProgressBar), new PropertyMetadata(null));
}