我正在尝试制作一个重复的文件检测器(相关问题:Compare adjacent list items)。我的一般结构是:
我遇到了第3步的问题。我的步骤1和2中的数据位于DupInfo对象列表中。这是类定义。
public class DupInfo
{
public string FullName { get; set; }
public long Size { get; set; }
public uint? CheckSum { get; set; }
public string BaseDirectory { get; set; }
public DupInfo(FileInfo file, Crc32 crc, int level)
{
FullName = file.FullName;
Size = file.Length;
CheckSum = crc.ComputeChecksum(File.ReadAllBytes(FullName));
BaseDirectory = FullName.Substring(0,FullName.NthIndexOf("\\",level));
}
public DupInfo(FileInfo file, int level)
{
FullName = file.FullName;
Size = file.Length;
BaseDirectory = FullName.Substring(0, FullName.NthIndexOf("\\", level));
}
}
我的问题是:
从自定义类(DupInfo)列表中将数据加载到Listview对象的最佳方法是什么?
如果Listview不是显示重复集的最佳工具,那么最好的是什么?
答案 0 :(得分:1)
我会使用绑定到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 { };
}
关于它。祝你好运!