Hallo Leute,
ich kann mir gerade etwas nicht erklären, ich möchte mittels:
C#-Quelltext
1:
| TreeViewItem item = (TreeViewItem)DieTreeView.ItemContainerGenerator.ContainerFromItem(fileItem); |
das TreeViewItem ermitteln und dann modifizieren.
Ich habe folgende Klassen:
Item:
C#-Quelltext
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12:
| public class Item :ICloneable { public string Name { get; set; } public string Path { get; set; } public Item() { }
public Object Clone() { return new Item { Name = this.Name, Path = this.Path }; } } |
DirectoryItem:
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40:
| public class DirectoryItem : Item, ICloneable { public ObservableCollection<Item> Items { get; set; }
public bool IsExpanded { get; set; }
public DirectoryItem() { Items = new ObservableCollection<Item>(); }
public new object Clone() { var newList = new ObservableCollection<Item>(GetAllItems(this.Items));
return new DirectoryItem { Name = this.Name, Path = this.Path, Items = newList, IsExpanded = this.IsExpanded }; }
private static ObservableCollection<Item> GetAllItems(ObservableCollection<Item> items) { var list = new ObservableCollection<Item>();
foreach (Item it in items) { if (it.GetType() == typeof(DirectoryItem)) { DirectoryItem dirIt = it as DirectoryItem; if (dirIt.Items.Count > 0) { DirectoryItem newDirItem = (DirectoryItem)dirIt.Clone(); newDirItem.Items = GetAllItems(dirIt.Items); list.Add(newDirItem); } } else { list.Add(new FileItem() { Name = it.Name, Path = it.Path }); } } return list; } } |
Und dann noch das FileItem welches mein Problem verursacht:
C#-Quelltext
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11:
| public class FileItem : Item , ICloneable { public List<string> KeywordList { get; set; }
public FileItem() { KeywordList = new List<string>(); }
public new FileItem Clone() { return new FileItem { Name = this.Name, Path = this.Path , KeywordList = this.KeywordList}; } } |
Im XAML sind die Klassen "FileItem" und "DirectoryItem" unter Resourcen folgend eingebunden:
XML-Daten
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13:
| <HierarchicalDataTemplate DataType="{x:Type Model:DirectoryItem}" ItemsSource="{Binding Items}"> <StackPanel Orientation="Horizontal"> <Image Width="28" Height="28" Source="Bild1.png"></Image> <TextBlock VerticalAlignment="Center" FontSize="16" Foreground="#FFC5C9CE" Margin="8,0,0,0" Text="{Binding Path=Name}" ToolTip="{Binding Path=Path}" /> </StackPanel> </HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type Model:FileItem}"> <StackPanel Orientation="Horizontal"> <Image Width="20" Height="20" Source="Bild2.png"></Image> <TextBlock FontSize="14" Foreground="#FFC5C9CE" Margin="8,0,0,0" Text="{Binding Path=Name}" ToolTip="{Binding Path=Path}" /> </StackPanel> </DataTemplate> |
Es funktionier auch soweit alles, nur wenn ich mir den Container eines FileItems holen möchte, klappt es nicht.
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32:
| void myContextItem_KeyWordSelectionChanged(object sender, List<string> keywords) { foreach (Item it in DieTreeView.Items) { if (it.GetType() == typeof(DirectoryItem)) { GetFileItems((DirectoryItem)it); } } }
private void GetFileItems(DirectoryItem dirItem) { foreach (Item it in dirItem.Items) { if (it.GetType() == typeof(DirectoryItem)) { GetFileItems((DirectoryItem)it); } else if (it.GetType() == typeof(FileItem)) CompareFileItemKeywordList((FileItem)it); } }
private void CompareFileItemKeywordList(FileItem fileItem) { TreeViewItem item = (TreeViewItem)SignalTreeView2.ThisTreeView.ItemContainerGenerator.ContainerFromItem(fileItem); item.Visibility = System.Windows.Visibility.Hidden; } |
Warum ist in der letzten Methode Item immer null, wenn folgende Mehode direkt funktioniert?????
C#-Quelltext
1: 2: 3: 4: 5: 6: 7: 8: 9:
| foreach (Item it in DieTreeView.Items) { if (it.GetType() == typeof(FileItem)) { TreeViewItem item = (TreeViewItem)SignalTreeView2.ThisTreeView.ItemContainerGenerator.ContainerFromItem((FileItem)it); item.Visibility = System.Windows.Visibility.Hidden; } } |
Kann hier jemand mein Problem erkennen? Das kann doch irgenwie nicht sein, oder????
Sorry das ich hier so viel Code poste, aber ich denke die Informationen sind notwendig.
Moderiert von
Christian S.: Code- durch XML-Tags ersetzt