I’m building a simple hex editor in C#, and I’ve decided to use each cell in a DataGrid to display a byte*. I know that DataGrid will take a list and display each object in the list as a row, and each of that object’s properties as columns. I want to display rows of 16 bytes each, which will require a wrapper with 16 string properties. While doable, it’s not the most elegant solution. Is there an easier way? I’ve already tried creating a wrapper around a public string array of size 16, but that doesn’t seem to work.
Thanks
*The rationale for this is that I can have spaces between each byte without having to strip them all out when I want to save my edited file. Also it seems like it’ll be easier to label the rows and columns.
I’d consider creating a regular Grid with 16 columns in
ColumnDefinitionsand dynamicRowDefinitionscollections and placing a borderlessTextBoxin each cell. You’d need to overload some key-handling to make it easier for user to navigate between text boxen, but the similar thing would be necessary for DataGrid’s cells.If you still want to use DataGrid, pointing
ItemsSourceto a list of 16-sized arrays should work. Create 16DataGridTextColumnsfrom code. Each one withBinding = new Binding(string.Format("[{0}]", i)), whereiis 0..15. Indexing property is just another property and you can easily bind to it. These don’t have to be string arrays: use bytes and create a IValueConverter between hex string and byte. Oh, and turn offAutoGenerateColumns.