1

I am building a file explorer in C# and WPF using a TreeView and a ListView, both bound to a class called DataItem. When I press the item that I want to import, it's children go to the ListView. But I can't set the selected item in the TreeView and expend it because the property SelectedItem of the TreeView is read-only. How do I set the item that I want to select to the TreeView from the ListView? I looked it up on the net and tried diffrent types of solutions, but with no luck.

My code for the data item (C#):

public abstract class DataItem
{
    private ObservableCollection<DataItem> FolderItems;

    public String Title {get; protected set; }

    public DataItem()
    {
        this.FolderItems = new ObservableCollection<DataItem>();

        this.Title = "";
    }

    public DataItem(DataItem Other) 
    {
        this.FolderItems = new ObservableCollection<DataItem>();

        this.Title = Other.Title;

        foreach (DataItem Folder in Other.FolderItems)
        {
            this.FolderItems.Add(Folder);
        }
    }

    public int Count()
    {
        int DataItemsCounter = 0;

        List<DataItem> FilesList = new List<DataItem>();

        foreach (DataItem File in this.Files)
        {
            FilesList.Add(File);

            DataItemsCounter++;
        }

        const int Empty = 0;

        const int FirstItem = 0;

        while (FilesList.Count != Empty)
        {
            DataItem File = FilesList[FirstItem];

            FilesList.RemoveAt(FirstItem);

            ObservableCollection<DataItem> Folder = File.Files;

            foreach (DataItem SubFile in Folder)
            {
                FilesList.Add(SubFile);

                DataItemsCounter++;
            }

        }

        return DataItemsCounter;
    }

    public virtual ObservableCollection<DataItem> Open()
    {
        return FolderItems;
    }

    public virtual ObservableCollection<DataItem> Files
    {
        get
        {
            return FolderItems;
        }
    }

    public ObservableCollection<DataItem> Folders
    {
        get
        {
            return FolderItems;
        }
    }

    public abstract String Format { get; }

    public String Icon
    {
        get
        {
            return String.Format("Icons/{0}.png", this.Format);
        }
    }
}

My code for the list view and the TreeView (WPF):

<TreeView x:Name="FileExplorerTreeView" SelectedItemChanged="FileExplorerTreeView_SelectedItemChanged">
                        <TreeView.ItemTemplate>
                            <HierarchicalDataTemplate DataType="{x:Type local:DataItem}" ItemsSource="{Binding Folders}">
                                <DockPanel>
                                    <TextBlock Name="CaptionTextblock" Text="{Binding Title}"  DockPanel.Dock="Right"/>
                                    <Image Source="{Binding Icon, Converter={StaticResource EmptyImageToImageSourceConverter}}" RenderOptions.BitmapScalingMode= "HighQuality" Stretch="Uniform" Height="{Binding ElementName=CaptionTextblock,Path=ActualHeight}" DockPanel.Dock="Left"/>
                                </DockPanel>
                            </HierarchicalDataTemplate>
                        </TreeView.ItemTemplate>
                    </TreeView>

                    <ListView x:Name="FileExplorerListView" Grid.Column="1" Margin="4,0,0,0" ItemsSource= "{Binding}" ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                          SelectedItem="{Binding DataItem, RelativeSource={RelativeSource AncestorType=Window}}" SelectionChanged="FileExplorerListView_SelectionChanged">
                        <ListView.ItemsPanel>
                            <ItemsPanelTemplate>
                                <WrapPanel/>
                            </ItemsPanelTemplate>
                        </ListView.ItemsPanel>

                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <DockPanel>
                                    <TextBlock DockPanel.Dock="Bottom" Text="{Binding Title}"/>
                                    <Image Source="{Binding Icon, Converter={StaticResource EmptyImageToImageSourceConverter}}" Width="32" Height="32" DockPanel.Dock="Top"/>
                                </DockPanel>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>
ouflak
  • 2,458
  • 10
  • 44
  • 49
  • 3
    Possible duplicate for: http://stackoverflow.com/questions/413890/how-to-programmatically-select-an-item-in-a-wpf-treeview – Timothy Ghanem Oct 22 '16 at 19:55
  • I already tried the multiple answers that was there, it didn't work and i didn't understood who to do part of them. – infoservice Oct 23 '16 at 07:22

1 Answers1

0

got it work, the setter of the item of the treeview didn't work because, the item itself was not shown on the treeview (his parents were collapsed) and i didn't implement the inotifypropertychanged interface so i change the item that when it's selected all of it's parents will be expended (needed to redo half of my project because of it) and i implemented the inotifypropertychanged interface, now it's work like a charm.