我目前有一个类myCommand
class myCommand : INotifyCollectionChanged , INotifyPropertyChanged
{
public string Name { get; set; }
public ObservableCollection<myFile> subCommand { get; set; }
}
我的窗口中有两个TreeView项。包含所有可用命令的tvCommandList和保存所有选定命令的tvFinalList。
我使用contextmenu将项目从tvCommandList复制到tvFinalList;在mainWindow中,有两个ObservableCollection项绑定到TreeViewItems。
ObservableCollection<myCommand> cmdlist = null;
ObservableCollection<myCommand> finallist = null;
它们绑定到XAML文件中的TreeView。
<Grid>
<Grid.Resources>
<ResourceDictionary>
<Style x:Key="styleTemplate" TargetType="TreeViewItem">
<Setter Property="IsSelected" Value="{Binding IsInitiallySelected, Mode=TwoWay}" />
</Style>
<HierarchicalDataTemplate DataType="{x:Type data:myCommand}"
ItemsSource="{Binding subCommand, Mode=TwoWay}">
<TextBlock Text="{Binding Name, Mode=TwoWay}" />
</HierarchicalDataTemplate>
</ResourceDictionary>
</Grid.Resources>
</Grid>
<TreeView x:Name="tvSendList" ItemsSource="{Binding}" DataContext="{Binding cmdlist}">
<TreeView x:Name="tvRecvList" ItemsSource="{Binding}" DataContext="{Binding finallist}">
我将TreeViewItem从cmdlist复制到finallist,并编辑它们以进行自定义数据。这里我面临的问题是,如果我在finallist中修改一个项(更新名称值),那么cmdlist项也会得到更新,我不知道如何解决这个问题。
我试着具体地将ResourceDictionary移动到每个TreeView,但仍然面临相同的问题。
发布于 2020-04-16 03:25:47
在创建集合的副本时,还应该克隆(即创建每个myFile
对象的副本),例如:
finalist = new ObservableCollection<myFile>(cmdlist.Select(x => new myFile()
{
Property1 = x.Property1,
Property2 = x.Property2
//copy all property values...
}));
https://stackoverflow.com/questions/61241782
复制