0

I'm new to WPF and C# coding. I'm trying to split 8000 bytes and display 32 byte per row. I write a SPLITLIST method, but don't know how to connect it to my dataGrid and create 250 (8000byte/32byte) separate rows. Below is my method:

 
public partial class DataInHex : Window
    {
        CalibrationHexDataDTO _calibrationHExDataDTO = new CalibrationHexDataDTO();
       
        const int SPLIT_SIZE = 32; //Size of the data in row
        public DataInHex(CalibrationHexDataDTO calibrationHexDataDTO)
        {
            InitializeComponent();
            this._calibrationHExDataDTO = calibrationHexDataDTO;
            dataInHexGrid.DataContext = calibrationHexDataDTO; //Here I get 8000 bytes of data

            SplitList();
        }
        public void SplitList() //Split the 8k bytes to 32 bytes per row
        {
            var list = new List<byte[]>();

            for (int i = 0; i < _calibrationHExDataDTO.Data.Length; i += SPLIT_SIZE)
            {
                byte[] splited = new byte[SPLIT_SIZE];
                // Array.Copy(_calibrationViewDTOs.Data,i,splited,0,SPLIT_SIZE);

                Buffer.BlockCopy(_calibrationHExDataDTO.Data, i, splited, 0, SPLIT_SIZE);
                list.Add(splited);
                
            }
       
        }
    }
Sep
  • 1
  • 1

1 Answers1

0

You can bind your data to a collection. To do so create a class first that holds your data. As far as I understood your requirement you only want to show one single item per row (of 32 bytes in size) probably in string format.

public class dataset
{
    private string _dataline;
    public dataset(string _data)
    {
        _dataline = _data;
    }

    public string DataLine
    {
        get{ return _dataline;}
        set{ _dataline = value;}
    }
}

Using the System.Collections namespace next define a ObservableCollection maintaining your data. It will be a Dependency property.

public ObservableCollection<dataset> DataLines
{
    get{return (ObservableCollection<DataLine>) GetValue(DataLineProperty);}
    set{ setValue(DataLinesProperty, value);}
}

Please note that the class "DataLine" is the data type of the ObservableCollection. The collection itself is accessed using the DataLines name. Next populate your DataLines Collection with data. To ease up writing I simply put your byte[] into a string array

dataset ds;
string[] eightKbytes;           // this is your data
DataLines = new ObservableCollection<dataset>;
for( int i = 0; i < 250; i++)
{
    ds = new dataset(eightKbytes[i]);
    DataLines.Add(ds);
}
eightkGrid.ItemsSource = DataLines;   // Set the items source here

In your XAML you define the DataGrid

<DataGrid :x:Name="eightkGrid" AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Header="My 32 ByteChunks", Binding="{Binding DataLine}"/>
    </DataGrid.Columns>
</DataGrid>

Hope that suits your requirements.

kmcontact
  • 21
  • 3