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>