C# 在绑定集合中向上/向下移动项目树视图中的重复项目
在绑定了C# 在绑定集合中向上/向下移动项目树视图中的重复项目,c#,wpf,data-binding,collections,treeview,C#,Wpf,Data Binding,Collections,Treeview,在绑定了TreeView的集合中上下移动对象时,正在移动的对象似乎在TreeView中重复(但不是绑定到的集合本身)。如果我关闭并重新打开包含TreeView的窗口,这种情况就会消失 我使用的是一个第三方.dll,它包含自己的可观察集合类,名为GeoCollection,看起来像: 公共类地理集合:集合、INotifyCollectionChanged、INotifyPropertyChanged 它包含用于MoveUp(string objName)和MoveDown(string objNa
TreeView
的集合中上下移动对象时,正在移动的对象似乎在TreeView
中重复(但不是绑定到的集合本身)。如果我关闭并重新打开包含TreeView
的窗口,这种情况就会消失
我使用的是一个第三方.dll,它包含自己的可观察集合类,名为GeoCollection
,看起来像:
公共类地理集合:集合、INotifyCollectionChanged、INotifyPropertyChanged
它包含用于MoveUp(string objName)
和MoveDown(string objName)
的函数,这两个函数明显地向上或向下移动集合中的项
我有一个GeoCollection
,其中Overlay
类包含一个GeoCollection
我正在使用一个层次数据模板
将它们绑定到一个树视图
,其中父节点将是覆盖层
,子节点将是层
。我正在使用ICollectionView
过滤这些覆盖
下面是我的xaml
:
<TreeView ItemsSource="{Binding OverlaysViewSource}" Name="LayersTreeView">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Layers}" >
<StackPanel Orientation="Horizontal">
<Label Content="{Binding Name}" />
</StackPanel>
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Label Content="{Binding Name}" />
</StackPanel>
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
下面是CollectionChanged的:
protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
{
NotifyCollectionChangedEventHandler collectionChanged = this.CollectionChanged;
if (collectionChanged != null)
{
collectionChanged(this, e);
}
}
更新2:我在上面提到过这一点,但是离开TreeView
并再次返回它会删除任何重复/错误,看起来很好
更新3:OnCollectionChanged
事件在移动项目时多次触发,我可以在最后一个事件中确认项目在集合中的顺序正确。听起来好像它们的INotifyCollectionChanged实现被破坏了。有没有可能只使用ObservableCollection?或者尝试移除并重新插入,而不是使用他们的方法:可能只是部分损坏。我将使用ObservableCollection测试它!我可能也会发一份bug报告。如果是我的密码,我想知道。同时检查文件(如有);也许他们做了一些非常奇怪的决定,他们不认为它做的任何事情都是“坏的”。如果是这样的话,您可能会对如何处理这些内容有一些见解。我猜如果这些项目在列表中
,您就可以这样做。在TabControl中,如果您通过ItemsSource填充它,则只有一个实际的ContentControl显示内容。当您来回导航时,它会重新加载模板XAML,刷新。说得委婉一点,这不是我设计TabControl的方式。所以它可以处理他们的集合,但你必须删除/重新插入?他们的解释基本上是,他们的开发人员没有费心做他们的工作?好吧,至少你可以解决这个问题。
public void MoveDown(int index)
{
ValidatorHelper.CheckIEnumerableIsNotNullNorEmpty((IEnumerable) this.mappingCache, "", "");
ValidatorHelper.CheckInputValueIsInRange((double) index, "index", 0.0, (double) (base.get_Items().Count - 1));
if ((index > 0) && (index < base.Count))
{
T local = base[index];
base.set_Item(index, base[index - 1]);
base.set_Item(index - 1, local);
if (this.mappingCache.ContainsKey(index))
{
this.dictionary.set_Item(this.mappingCache[index], base[index - 1]);
}
if (this.mappingCache.ContainsKey(index - 1))
{
this.dictionary.set_Item(this.mappingCache[index - 1], base[index]);
}
string str = this.mappingCache[index];
this.mappingCache.set_Item(index, this.mappingCache[index - 1]);
this.mappingCache.set_Item(index - 1, str);
List<object> list1 = new List<object>();
list1.Add(local);
this.OnCollectionChanged((NotifyCollectionChangedEventArgs) new PclNotifyCollectionChangedEventArgs(PclNotifyCollectionChangedAction.Move, (IList) list1, index - 1, index));
}
}
protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
{
NotifyCollectionChangedEventHandler collectionChanged = this.CollectionChanged;
if (collectionChanged != null)
{
collectionChanged(this, e);
}
}