views:

25

answers:

2

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.

+1  A: 

I'd consider creating a regular Grid with 16 columns in ColumnDefinitions and dynamic RowDefinitions collections and placing a borderless TextBox in 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 ItemsSource to a list of 16-sized arrays should work. Create 16 DataGridTextColumns from code. Each one with Binding = new Binding(string.Format("[{0}]", i)), where i is 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 off AutoGenerateColumns.

repka
The regular grid sounds easiest to implement. I had actually thought of doing something similar when I noticed the DataGrid control and thought it would be easier.
Joel
+1  A: 

I suggest you to use MultiBinding.

     <my:DataGrid ItemsSource="{Binding HexDataArray}">
        <my:DataGrid.Columns>
            <my:DataGridTemplateColumn>
                <my:DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock>
                          <TextBlock.Text>
                            <MultiBinding StringFormat="{}{0} {1} ...">
                              <Binding Path="Array[0]" />
                              <Binding Path="Array[1]" />
                                .........................
                            </MultiBinding>
                          </TextBlock.Text>
                        </TextBlock>
                    </DataTemplate>
                </my:DataGridTemplateColumn.CellTemplate>
            </my:DataGridTemplateColumn>
        </my:DataGrid.Columns>
    </my:DataGrid>
Levelbit