我有一个自定义列表:
public class HardwareTable : IEquatable<HardwareTable>
{
public byte SectorNumber { get; set; }
public byte SectionNumber { get; set; }
public byte SectionStatus { get; set; }
public override string ToString()
{
return "Sector Number: " + SectorNumber;
}
public override bool Equals(object obj)
{
if (obj == null) return false;
HardwareTable objAsPart = obj as HardwareTable;
if (objAsPart == null) return false;
else return Equals(objAsPart);
}
public override int GetHashCode()
{
return SectorNumber;
}
public bool Equals(HardwareTable other)
{
if (other == null) return false;
return (this.SectorNumber.Equals(other.SectorNumber));
}
}
我用我的东西填充
List<HardwareTable> ImpDataList = new List<HardwareTable>();
ImpDataList.Add(new ImportedDataList(){ SectorNumber=1,SectionNumber=3,SectionStatus=0}
ImpDataList.Add(new ImportedDataList(){ SectorNumber=2,SectionNumber=3,SectionStatus=0}
ImpDataList.Add(new ImportedDataList(){ SectorNumber=1,SectionNumber=5,SectionStatus=0}
ImpDataList.Add(new ImportedDataList(){ SectorNumber=1,SectionNumber=5,SectionStatus=1}
....
我有一个动态创建的数据网格:
TabItem ReturnedData = new TabItem();
ReturnedData.Header = "Returned Data";
var ReturnedDataGrid = new DataGrid();
ReturnedDataGrid.AlternatingRowBackground = Brushes.LightBlue;
ReturnedDataGrid.CanUserSortColumns = true;
ReturnedDataGrid.IsReadOnly = true;
ReturnedDataGrid.ItemsSource =ImpDataList; // When I do this it works but I cannot sort the data, also the data are not sorted when they are displayed.
ReturnedData.Content = ReturnedDataGrid;
tabControl.Items.Add(ReturnedData);
使用上述方法我显示数据,但排序不起作用。我可以理解我需要将List绑定到数据网格,但到目前为止,我已经尝试了很多方法,但非对我的情况有效。
一种方法建议我这样做:
CollectionViewSource itemCollectionViewSource;
itemCollectionViewSource = (CollectionViewSource)FindResource("ItemCollectionViewSource"));
itemCollectionViewSource.Source = ImpDataList;
ReturnedDataGrid.DataContext = "{StaticResource ItemCollectionViewSource}";
ReturnedDataGrid.ItemsSource = "{Binding}";
但我甚至无法显示我的清单。它显示空单元格。至少在最初的方法中,我可以显示数据,尽管它们没有被排序。
有关如何进行的任何建议吗?