Witam
Jestem nowa w WPFie, wcześniej używałam winforms więc wiązanie danych to dla mnie coś nowego.
Stąd mam taki problem.
Chcę użyć poniższej kontrolki w swoim projekcie:
http://www.codeproject.com/Articles/24973/TreeListView
Dostarczony tam projekt pokazuje przykładowe użycie tej kontrolki. Przekopiowałam do swojego projektu Themes/Generic.xaml, TreeListView.cs oraz xamla z MainWindow.xaml.
Kod przykładowy działa dobrze i wiąże dane statycznie:
<local:TreeListView AllowsColumnReorder="True">
<!--Create an item template to specify the ItemsSource-->
<local:TreeListView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Children}" />
</local:TreeListView.ItemTemplate>
<local:TreeListView.Columns>
<!--Create the first column containing the expand button and the type name.-->
<GridViewColumn Header="Name" Width="200">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<!--The Expander Button (can be used in any column (typically the first one))-->
<local:TreeListViewExpander/>
<!--Display the name of the DataElement-->
<TextBlock Text="{Binding}"/>
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<!--Create a second column containing the number of children.-->
<GridViewColumn Header="Children" Width="100">
<GridViewColumn.CellTemplate>
<DataTemplate>
<!--Display the size of the DataElement-->
<TextBlock Text="{Binding Children.Count}" HorizontalAlignment="Right"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<!--Create a third column containing the brush of the material.-->
<GridViewColumn Header="Brush" Width="100">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<!--Border showing the actual color-->
<Border Background="{Binding Brush}" CornerRadius="2"
Width="16" Height="16"
BorderThickness="1" BorderBrush="DarkGray"/>
<!--Display the brush-->
<TextBlock Text="{Binding Brush}"/>
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</local:TreeListView.Columns>
<!--Create some sample data-->
<MaterialGroup>
<MaterialGroup>
<DiffuseMaterial Brush="Blue"/>
<DiffuseMaterial Brush="Red"/>
<SpecularMaterial Brush="Orange"/>
</MaterialGroup>
<EmissiveMaterial Brush="AliceBlue"/>
</MaterialGroup>
</local:TreeListView>
Ja potrzebuje zamienić wyświetlanie tych MaterialGroup na wyświetlanie drzewa plików i katalogów. W kolumnach chce odpowiednio nazwę, ilość plików, rozmiar.
Znalazłam taką klasę: ItemProvider:
using System.Collections.Generic;
using System.IO;
namespace wp0504
{
public class Item
{
public string Name { get; set; }
public string Path { get; set; }
}
public class FileItem : Item
{
}
public class DirectoryItem : Item
{
public List<Item> Items { get; set; }
public DirectoryItem()
{
Items = new List<Item>();
}
}
public class ItemProvider
{
public List<Item> GetItems(string path)
{
var items = new List<Item>();
var dirInfo = new DirectoryInfo(path);
foreach (var directory in dirInfo.GetDirectories())
{
var item = new DirectoryItem
{
Name = directory.Name,
Path = directory.FullName,
Items = GetItems(directory.FullName)
};
items.Add(item);
}
foreach (var file in dirInfo.GetFiles())
{
var item = new FileItem
{
Name = file.Name,
Path = file.FullName
};
items.Add(item);
}
return items;
}
}
}
i w konstruktorze MainWidnow dodałam jej instancje wskazującą na folder obrazów, do datacontext:
var itemProvider = new ItemProvider();
var items = itemProvider.GetItems(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures));
DataContext = items;
Niestety nie mam pojęcia jak wyedytować plik XAML aby móc wyświetlać te dane z ItemProvider'a. Wszelkie moje próby zakończyły się niepowodzeniem. Pomoże ktoś?