0

I need to display a memory dump for a technical application. Each Byte (Cell) should be defined via a DataTemplate to show additional information (highlight via setting Background color, individual Tooltip etc). I made the following attempt:

  <DataTemplate x:Key="HexNumberTemplate">
    <Grid>
      [...]
      <TextBlock>
        <TextBlock.Text>
          <Binding Path="Cell[0].Value">
            <Binding.Converter>
              [...]
            </Binding.Converter>
          </Binding>
        </TextBlock.Text>
      </TextBlock>
    </Grid>
  </DataTemplate>

The final result should look like this:
Dump example

My problem is the fix coded Binding path. 'Cell' is a list of objects that holds all necessary information to display the cell. Using this approach, I need to define 16 times the same DataTemplate with Cell[0] to Cell[15]. I definitely want to avoid this!
I read an approach defining the DataTemplate in source code where I assemble the XAML in a string and call Markup.XamlReader.Load(MemoryStreamOfTheString). But here I lose the comfort of the Visual Studio IDE.
Is it possible to define the DataTemplate in XAML and make the indexer of the Cell-Object a parameter?

4 Answers 4

2

You should do like you have read: create templates dynamically, by loading them with XamlReader. In order to have comfort of XAML editor, you can define your template in a separate xaml file like this:

<DataTemplate
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid DataContext="{Binding Current_Cell}">
        <!--Your template controls goes here.-->
    </Grid>
</DataTemplate>

Then set type of this file to Resource, and load it into string and simply replace Current_Cell with each individual cell numbers before you load template from string when you construct your view. By setting DataContext of Grid you help yourself to use other binding inside the template (context is already set to the current cell, and you don't need to replace it everywhere).

I was in a the same situation recently, only difference was, that my grid had totally dynamic columns (loaded from server), so I didn't even have the opportunity to create 16 templates :)

Sign up to request clarification or add additional context in comments.

2 Comments

+1 for the (simple) idea to export it into another XAML to have the IDE comfort. What I have not mentioned is, that I have an unkown amount of columns as you had. The picture of a common memory layout here was just to simplify the question for SO. (If the question is too complex, you won't get any answers.)
I think this is the way I want to go in my case. As far as I read here (support.microsoft.com/kb/319292), there's a need to set it to Embedded Resource.
1

Try it with ListBoxes.

The outer ListBox includes the rows which are ListBoxes too, each of them binded to a List object. And you can create the DataTemplate of the ListBoxItems.

<DataTemplate x:Key="innerListBoxItem">
    [...]
    <TextBlock Text="{Binding Value}" />
    [...]
<DataTemplate>

<DataTemplate x:key="outerListBoxItem">
    <Grid>
        <ListBox ItemTemplate="{StaticResource innerListBoxItem}" ItemCollection="{Binding Cells}"/>
    </Grid>
<DataTemplate>

and whereever you want to put this control:

<ListBox ItemTemplate="{StaticResource outerListBoxItem}" ItemCollection={Binding CellsList}"/>

code behind:

public class ListOfCells { public List<Cell> Cells {get; set; } }

public List<ListOfCells> CellsList {get; private set; }

2 Comments

And don't forget to set the outerListBoxItems lists ItemsPanel to a template with Horizontal aligned StackPanel
Ah! Yes of course. An inner ListBox can iterate through the list of cells. Thanks for the new idea.
1

You can try and use the Attached Behavior pattern. You can bind an attached property to the column number, and the attached behavior will bind the text to the required cell given the column number.

1 Comment

+1 for the very interesting link. I want to think about it some more time.
0

I would suggest to use a single column DataGrid with custom Header and Cell templates. You grid won't benefit from indivudual cells resizing, will it? Your header is going to have a fixed number of columns, you cell template can be implemented as a subclass of ListControl - we just need to change StackPanel's orientation from vertical to horizontal. Then, your bound object will be a collection of bytes, which is easy as your cell control is derived from ListControl.

Please let us know if that makes sense.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.