I would use a DataGrid bound to an ObservableCollection<DupInfo>
.
<Window x:Class="DataGridDupInfoStack.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">
<Grid>
<DataGrid ItemsSource="{Binding items}">
</DataGrid>
</Grid>
</Window>
Codebehind:
public partial class MainWindow : Window
{
public ObservableCollection<DupInfo> items { get; set; }
public MainWindow()
{
InitializeComponent();
items = new ObservableCollection<DupInfo>();
items.Add(new DupInfo() { BaseDirectory = "Directory1", CheckSum = 0xFF, FullName = "Info1", Size = 100 });
items.Add(new DupInfo() { BaseDirectory = "Directory2", CheckSum = 0xFE, FullName = "Info2", Size = 150 });
items.Add(new DupInfo() { BaseDirectory = "Directory2", CheckSum = 0xFD, FullName = "Info3", Size = 200 });
this.DataContext = this;
}
}
And this is your DupInfo class. I've omitted the constructor to deal with it faster.
public class DupInfo
{
public string FullName { get; set; }
public long Size { get; set; }
public uint? CheckSum { get; set; }
public string BaseDirectory { get; set; }
}
Result:

Update 1:
We don't have AddRange available but define an extenstion method for that:
public static class ExtensionMethods
{
public static void AddRange(this ObservableCollection<DupInfo> value, List<DupInfo> list)
{
foreach (var dup in list)
value.Add(dup);
}
}
This is our new version:
public partial class MainWindow : Window
{
public ObservableCollection<DupInfo> items { get; set; }
List<DupInfo> initialList { get; set; }
public MainWindow()
{
InitializeComponent();
items = new ObservableCollection<DupInfo>();
initialList = new List<DupInfo>();
initialList.Add(new DupInfo() { BaseDirectory = "Directory1", CheckSum = 0xFF, FullName = "Info1", Size = 100 });
initialList.Add(new DupInfo() { BaseDirectory = "Directory2", CheckSum = 0xFE, FullName = "Info2", Size = 150 });
initialList.Add(new DupInfo() { BaseDirectory = "Directory2", CheckSum = 0xFD, FullName = "Info3", Size = 200 });
items.AddRange(initialList);
this.DataContext = this;
}
}
public static class ExtensionMethods
{
public static void AddRange(this ObservableCollection<DupInfo> value, List<DupInfo> list)
{
foreach (var dup in list)
value.Add(dup);
}
}
So we are loading our elements from a list .. we can use an array there or whatever else according to your needs.
But pay attention if you change the object on which our reference points to. In that case, you will have to use a DependencyProperty or implement INotifyPropertyChanged.
Your property will look like this:
public ObservableCollection<DupInfo> items
{
get { return ( ObservableCollection<DupInfo>)GetValue(itemsProperty); }
set { SetValue(itemsProperty, value); }
}
// Using a DependencyProperty as the backing store for items. This enables animation, styling, binding, etc...
public static readonly DependencyProperty itemsProperty =
DependencyProperty.Register("items", typeof( ObservableCollection<DupInfo>), typeof(MainWindow), new PropertyMetadata(null));
Update 2:
XAML:
<Window x:Class="DataGridDupInfoStack.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">
<Grid>
<StackPanel>
<DataGrid ItemsSource="{Binding items}" AutoGenerateColumns="False" CanUserAddRows="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Full Name" Binding="{Binding FullName}" />
<DataGridTextColumn Header="Size" Binding="{Binding Size}" />
<DataGridTextColumn Header="CheckSum" Binding="{Binding CheckSum}" />
<DataGridTextColumn Header="BaseDirectory" Binding="{Binding BaseDirectory}" />
<DataGridTemplateColumn Header="Mark for deletion">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding Path=ToDelete, UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
<Button Content="Delete" Click="btnDelete_Click"/>
</StackPanel>
</Grid>
</Window>
Codebehind:
public partial class MainWindow : Window
{
public ObservableCollection<DupInfo> items
{
get { return (ObservableCollection<DupInfo>)GetValue(itemsProperty); }
set { SetValue(itemsProperty, value); }
}
// Using a DependencyProperty as the backing store for items. This enables animation, styling, binding, etc...
public static readonly DependencyProperty itemsProperty =
DependencyProperty.Register("items", typeof(ObservableCollection<DupInfo>), typeof(MainWindow), new PropertyMetadata(null));
List<DupInfo> initialList { get; set; }
public MainWindow()
{
InitializeComponent();
items = new ObservableCollection<DupInfo>();
initialList = new List<DupInfo>();
initialList.Add(new DupInfo() { BaseDirectory = "Directory1", CheckSum = 0xFF, FullName = "Info1", Size = 100 });
initialList.Add(new DupInfo() { BaseDirectory = "Directory2", CheckSum = 0xFE, FullName = "Info2", Size = 150 });
initialList.Add(new DupInfo() { BaseDirectory = "Directory2", CheckSum = 0xFD, FullName = "Info3", Size = 200 });
items.AddRange(initialList);
this.DataContext = this;
}
private void btnDelete_Click(object sender, RoutedEventArgs e)
{
foreach (var dup in items.ToList())
{
if (dup.ToDelete)
{
items.Remove(dup);
}
}
}
}
public static class ExtensionMethods
{
public static void AddRange(this ObservableCollection<DupInfo> value, List<DupInfo> list)
{
foreach (var dup in list)
value.Add(dup);
}
}
And your updated DupInfo class:
public class DupInfo : INotifyPropertyChanged
{
private bool _ToDelete;
public bool ToDelete
{
get { return _ToDelete; }
set
{
_ToDelete = value;
PropertyChanged(this, new PropertyChangedEventArgs("ToDelete"));
}
}
public string FullName { get; set; }
public long Size { get; set; }
public uint? CheckSum { get; set; }
public string BaseDirectory { get; set; }
public event PropertyChangedEventHandler PropertyChanged = delegate { };
}
That's about it. Good luck!