当我在 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,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,etc...
    public static readonly DependencyProperty RangesProperty =
        DependencyProperty.Register("Ranges",typeof(List<HttpRange>),new PropertyMetadata(null));

}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)