如何使用c#(WPF)将一维字节数组转换为5列的DataGrid?

cgh8pdjw  于 2023-08-07  发布在  C#
关注(0)|答案(1)|浏览(95)
public partial class MainWindow : Window
    {
        public MainWindow()
        {
            BinaryReader br = new BinaryReader(File.Open(FILE_NAME, FileMode.Open));
            long dataLength = br.BaseStream.Length;
            byte[] a = new byte[br.BaseStream.Length];
            a= br.ReadBytes((int)br.BaseStream.Length);

            InitializeComponent();
        }
....

字符串
readData是从二进制文件中读取的字节数组。我们如何使用c#将这个字节数组变成一个5列的DataGrid?
就像这样

position| 0  | 1  | 2  | 3  | 4  |
0       |a[0]|a[1]|a[2]|a[3]|a[4]|
1       |a[5]|a[6]|a[7]|a[8]|a[9]|


...
我尝试创建一个9字节的对象,并使用ListView来实现,但我认为这不适合未来的工作...

public class Data
    {
        public int position { get; set; }
        public byte _0 { get; set; }
        public byte _1 { get; set; }
        public byte _2 { get; set; }
        public byte _3 { get; set; }
        public byte _4 { get; set; }
        public byte _5 { get; set; }
        public byte _6 { get; set; }
        public byte _7 { get; set; }
        public byte _8 { get; set; }
        public byte _9 { get; set; }
        public byte _A { get; set; }
        public byte _B { get; set; }
        public byte _C { get; set; }
        public byte _D { get; set; }
        public byte _E { get; set; }
        public byte _F { get; set; }
    }

wixjitnu

wixjitnu1#

如果你的数组不是太大,你可以使用以下基于LINQ的解决方案。否则,您必须创建一个生成器(使用yield return的枚举流)。
您应该使用异步File API来提高性能使用File.ReadAllBytesAsync读取二进制文件。BinaryReader不再是常见场景所必需的(注意,BinaryReaderFileStream都实现了IDisposable,并且您发布的代码不会处理这两个示例!).
MainWíndow.xaml**

<Window>
  <DataGrid x:Name="BytesTable" />
</Window>

字符串

主窗口.xaml.cs

partial class MainWindow : Windwo
{
  public MainWindow()
  {
    InitilaizeComponent();
    this.Loaded += OnLoaded;
  }

  private async void OnLoaded(object sender, RoutedEventArgs e)
  {
    // Define the desired column count.
    // The source array will be partitioned accordingly.
    // An extra column to display the row index is additionally added to the table.
    int columnCount = 5;

    // Asynchronously read the binary file
    byte[] bytes = await File.ReadAllBytesAsync(FILE_NAME);

    // Create the DataGrid data source
    var dataTable = new DataTable();

    // Add the "position" column that maps to the row index
    dataTable.Columns.Add("position", typeof(int));

    // Add the byte value columns
    for (int columnIndex = 0; columnIndex < columnCount; columnIndex++)
    {
      dataTable.Columns.Add(columnIndex.ToString(), typeof(byte));
    }

    // Split the array into groups of n long collections using LINQ.
    // Each group represents a row of n values/cells.
    var groupedValues = bytes
      .Select((item, itemIndex) => (itemIndex, item))
      .GroupBy(itemInfo => Math.Floor(itemInfo.itemIndex / (double)columnCount))
      .Select(group => group.Select(groupvalue => groupvalue.item));

    // Populate the table rows using the grouped collections.
    // Each group represents a row of n values, where n is '5' in your example
    for (int rowIndex = 0; rowIndex < groupedValues.Count(); rowIndex++)
    {
      var rowData = groupedValues.ElementAt(rowIndex);
      DataRow newRow = dataTable.NewRow();
      newRow.ItemArray = new List<byte>{ (byte)rowIndex }
        .Concat(rowData)
        .Cast<object>()
        .ToArray();
      dataTable.Rows.Add(newRow);
    }

    this.BytesTable.ItemsSource = dataTable.DefaultView;
  }
}

相关问题