首页 > 解决方案 > 当我在 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));

}

标签: wpfbindingitemscontrol

解决方案


推荐阅读