我会使用绑定到ObservableCollection<DupInfo> 的DataGrid。
<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>
代码隐藏:
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;
}
}
这是你的 DupInfo 类。我省略了构造函数以更快地处理它。
public class DupInfo
{
public string FullName { get; set; }
public long Size { get; set; }
public uint? CheckSum { get; set; }
public string BaseDirectory { get; set; }
}
结果:
更新 1:
我们没有可用的 AddRange,但为此定义了一个扩展方法:
public static class ExtensionMethods
{
public static void AddRange(this ObservableCollection<DupInfo> value, List<DupInfo> list)
{
foreach (var dup in list)
value.Add(dup);
}
}
这是我们的新版本:
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);
}
}
所以我们从一个列表中加载我们的元素......我们可以在那里使用一个数组或根据您的需要使用其他任何东西。
但是如果你改变了我们的引用指向的对象,请注意。在这种情况下,您将不得不使用 DependencyProperty 或实现 INotifyPropertyChanged。
您的财产将如下所示:
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));
更新 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>
代码隐藏:
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);
}
}
还有你更新的 DupInfo 类:
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 { };
}
就是这样。祝你好运!