ListBox Expander Binding WPF Example
XAML
<Window x:Class="ImageManager.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<Grid>
<ListBox Name="lst" Margin="5"
ItemsSource="{Binding}"
HorizontalContentAlignment="Stretch">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Margin="5" FontSize="16" FontWeight="Bold" Text="{Binding Path=Name}"/>
<Expander Name="expender">
<Border Margin="5" Background="Wheat" BorderThickness="2" BorderBrush="#FF7A3E3E">
<StackPanel>
<TextBlock Margin="5, 0" Foreground="Blue" Text="{Binding Path=FreeSpace}"/>
<TextBlock Margin="5, 0" Foreground="Blue" Text="{Binding Path=Type}"/>
</StackPanel>
</Border>
</Expander>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace ImageManager
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public class FileInfo
{
public FileInfo(string driveName, string freeSpace, string type)
{
Name = driveName;
FreeSpace = "Free space: "
+ freeSpace;
Type = "Type: " + type;
}
public string Name
{ get; set; }
public string FreeSpace
{ get; set; }
public string Type
{ get; set; }
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
List<FileInfo> states = new List<FileInfo>();
DriveInfo[] ListDrives = DriveInfo.GetDrives();
foreach (DriveInfo Drive in ListDrives)
{
if(Drive.IsReady )
states.Add(new FileInfo(Drive.Name,
Drive.AvailableFreeSpace.ToString(), Drive.DriveType.ToString()) );
}
lst.ItemsSource = states;
}
void FileDirSearch(string sDir, List<string> sfiles, string pattern)
{
try
{
foreach (string d in Directory.GetDirectories(sDir))
{
foreach (string f in Directory.GetFiles(d,
pattern))
{
sfiles.Add(f);
}
FileDirSearch(d, sfiles,
pattern);
}
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
}
}
}