wpf - 当我在 ItemsSource 中添加新项目时,WPF ItemsControl 不更新
问题描述
我想为多线程下载器制作一个 PartialProgressBar,但我无法让它工作。将元素添加到 ItemsSource 时没有任何变化。我该如何解决?
主窗口。
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);
}
}
主窗口.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));
}
解决方案
推荐阅读
- python - 在python中的一个绘图(叠加)中绘制两个轮廓
- string - 无法在 BASH 中使用 CURL 向服务器发布请求
- c++ - 如何限制从 int 到 enum 类的转换?
- android - google 的 Map SDK 计费如何适用于 Android 和 iOS 应用程序?
- azure-data-explorer - 如何在 Azure 数据资源管理器 (kusto) 中从一个表查找值到另一个表
- javascript - 如何验证 vue.js 中的动态输入字段?
- javascript - 如何使字段验证在自定义策略自定义 ui 上正常工作?
- oracle - 用于 oracle、db2 和 sap 的测试数据管理解决方案
- android - 谷歌地图未显示在底部导航活动片段(kotlin)中
- html - 如何更改引导程序 4 选择器的默认下拉颜色?