Autor Beitrag
Christoph1972
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 690
Erhaltene Danke: 16


VS2015 Pro / C# & VB.Net
BeitragVerfasst: So 24.11.13 22:14 
Hallo Leute,

ich kann mir gerade etwas nicht erklären, ich möchte mittels:

ausblenden C#-Quelltext
1:
TreeViewItem item = (TreeViewItem)DieTreeView.ItemContainerGenerator.ContainerFromItem(fileItem);					


das TreeViewItem ermitteln und dann modifizieren.

Ich habe folgende Klassen:

Item:
ausblenden 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:
ausblenden volle Höhe C#-Quelltext
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);//Rekursion
                    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:
ausblenden 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:

ausblenden 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.

ausblenden volle Höhe C#-Quelltext
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:
//hier startet das Prozedere....(Ein Filteraudruck in einem Kentextmenue wurde geändert)
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);//Rekursion da Unterordner vorhanden sind.
        }
        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;
    
    //in dieser Methode ist Item immer null.
}



Warum ist in der letzten Methode Item immer null, wenn folgende Mehode direkt funktioniert?????

ausblenden 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 user profile iconChristian S.: Code- durch XML-Tags ersetzt

_________________
Gruß
Christoph
Christoph1972 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 690
Erhaltene Danke: 16


VS2015 Pro / C# & VB.Net
BeitragVerfasst: So 24.11.13 23:15 
Etwas blöd, aber so ist es:

TreeView.ItemContainerGenerator.ContainerFromItem returns null

Morgen gehts weiter :-)

Moderiert von user profile iconTh69: URL-Titel gesetzt.

_________________
Gruß
Christoph
Christoph1972 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 690
Erhaltene Danke: 16


VS2015 Pro / C# & VB.Net
BeitragVerfasst: Di 26.11.13 22:51 
Ich wollte nur noch mal kurz mitteilen, das man die Geschichte nicht vernünftig zum Laufen bringen kann. Rekursion funktioniert einfach nicht. Auch nicht mit den genannten Hilfen alla .IsExpanded und .Focus(). Schade, ich hätte so gerne die TreeviewItems direkt manipuliert, aber gut.......

_________________
Gruß
Christoph