Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从自己的集合中自动更新ItemsSource?_C#_Xaml_Uwp_Uwp Xaml - Fatal编程技术网

C# 如何从自己的集合中自动更新ItemsSource?

C# 如何从自己的集合中自动更新ItemsSource?,c#,xaml,uwp,uwp-xaml,C#,Xaml,Uwp,Uwp Xaml,我已经创建了自己的集合并实现了INotifyCollectionChanged public class ObservableSortedSet<T> : SortedSet<T>, INotifyCollectionChanged { public event NotifyCollectionChangedEventHandler CollectionChanged; public new bool Add(T item) {

我已经创建了自己的集合并实现了INotifyCollectionChanged

public class ObservableSortedSet<T> : SortedSet<T>, INotifyCollectionChanged
{
    public event NotifyCollectionChangedEventHandler CollectionChanged;

    public new bool Add(T item)
    {
        var result = base.Add(item);
        if (result)
            CollectionChanged?.Invoke(item,
                new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item));
        return result;
    }

    public new bool Remove(T item)
    {
        var result = base.Remove(item);
        if (result)
            CollectionChanged?.Invoke(item,
                new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item));
        return result;
    }

    public new void Clear()
    {
        base.Clear();
        CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
    }
}
公共类ObserviesOrtedSet:SortedSet,INotifyCollectionChanged
{
公共事件通知CollectionChangedEventHandler CollectionChanged;
公共新bool添加(T项)
{
var结果=基础。添加(项目);
如果(结果)
CollectionChanged?调用(项,
新的NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add,item));
返回结果;
}
公共新bool删除(T项)
{
var结果=基础。移除(项目);
如果(结果)
CollectionChanged?调用(项,
新建NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove,item));
返回结果;
}
公共新空白清除()
{
base.Clear();
CollectionChanged?.Invoke(这是新的NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}
}

但是,当我尝试在视图中将此集合用作ItemsSource时,它们不会在删除项目时自动更新。正如我在这里的其他问题中所看到的,我应该实现INotifyCollectionChanged。我这样做了,但它不起作用。有什么建议吗?

您的
remove
方法不起作用的原因是您必须添加已删除元素的索引

我试过这种方法,它确实有效:

查看代码隐藏:

public ObservableSortedSet<String> Values { get; private set; }

    public MainWindow()
    {
        InitializeComponent();
        DataContext = this;

        Values = new ObservableSortedSet<string>();
        Values.Add("Test0");
        Values.Add("Test1");
        Values.Add("Test2");
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        Values.Add("Test" + Values.Count);
    }
}
public observesOrtedSet值{get;private set;}
公共主窗口()
{
初始化组件();
DataContext=this;
值=新的ObserviesOrtedSet();
添加(“Test0”);
添加(“测试1”);
添加(“测试2”);
}
私有无效按钮\u单击(对象发送者,路由目标e)
{
值。添加(“测试”+值。计数);
}
}
视图:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>

    <ListView ItemsSource="{Binding Path=Values}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <Label Content="{Binding}" />
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

    <Button Grid.Row="1" Content="Add" Click="Button_Click"/>
</Grid>


是否尝试删除某个项目?是的,当添加已删除元素的索引时,删除会起作用。我想我应该将此添加到CollectionChanged的调用中。如何获取已删除项目的索引,或者仅使用0?这就是问题所在:数据将不起作用,不管怎样,我将手动刷新我的项目资源。谢谢你的帮助。