[英]Binding a TreeView to a collection with different types of objects on same level
[英]Binding different objects for different TreeView based on same class getting linked
我目前有一個class myCommand
class myCommand : INotifyCollectionChanged , INotifyPropertyChanged
{
public string Name { get; set; }
public ObservableCollection<myFile> subCommand { get; set; }
}
我的 window 中有兩個 TreeView 項目。 tvCommandList包含所有可用的命令和tvFinalList保存所有選定的命令。
我使用 contextmenu 將項目從tvCommandList復制到tvFinalList ; 在主窗口中,我有兩個綁定到 TreeViewItems 的 ObservableCollection 項。
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項目也會更新,我不知道如何 go 來解決這個問題。
我嘗試將 ResourceDictionary 具體移動到每個 TreeView ,但仍然面臨同樣的問題
當您創建集合的副本時,您還應該克隆,即創建每個單獨的myFile
object 的副本,例如:
finalist = new ObservableCollection<myFile>(cmdlist.Select(x => new myFile()
{
Property1 = x.Property1,
Property2 = x.Property2
//copy all property values...
}));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.