Question

I am working on a project for modifying a specific sort of binary file format. Part of the specification is to allow XML definitions of struct layouts, like so:

<!-- Invalid plugin, for demonstration only -->
<plugin name="weap">
    <struct size="0x504">
        <float32 name="initial_rounds_per_second" offset="0x4" />
        <float32 name="final_rounds_per_second" offset="0x8" />
        <float32 name="rounds_per_second_acceleration" offset="0xA" />
        <float32 name="rounds_per_second_deceleration" offset="0xC" />
        <uint16 name="projectiles_per_shot" offset="0x6E" />
        <array name="magazines" offset="0xF0">
            <struct size='112'>
                <uint16 name="rounds_recharged" offset="0x4" />
                <uint16 name="rounds_total_initial" offset="0x6" />
                <uint16 name="rounds_total_maximum" offset="0x8" />
                <uint16 name="rounds_loaded_maximum" offset="0xA" />
            </struct>
        </array>
    </struct>
</plugin>

These XML plugins are loaded at runtime and used to interpret the binary file. I have a working prototype in Python which, at runtime, interprets these plugins and generates classes based on them.

My trouble is with WPF databinding -- WPF needs to know the names of the properties it will bind to at compile-time. I would like to databind the WPF GUI to these plugin-defined data structures, so I'm in a bit of a pickle.

How can I databind to properties whose names I don't know until runtime?

The only tentative solution I've been able to think of is storing all of this information in a "Fields" property of type Dictionary<String,Object>, where the string => object mapping represents the named elements. WPF can then bind to this Dictionary, using data templates to list out each key/value pair.

However, my inexperience with WPF puts me on shaky ground, and my solution raises a number of other questions:

  • Is this Dictionary approach a good way to solve the problem?
  • Which observable datatype ought I to use?
  • If one value in the dictionary is updated, does WPF need to refresh the whole collection?
  • Will I have performance issues if the dictionary consists of hundreds of key/value pairs?
Was it helpful?

Solution 2

Your Dictionary approach is perfectly fine. I use in some places a Dictionary < object, ObservableCollection < string > >. This works well as long as you understand its limitations, such as that if you modify one of the collections (delete, insert, remove, etc...), you won't have the built-in notifications from the dictionary that you get with the observable collection itself. If you want a Dictionary that itself behaves as an 'observable' dictionary, you will have to create a custom class. I've done this and it works well with a few tweaks.

As far as performance is concerned, well it really depends on how you are using it. My custom dictionary is pretty much analog to the performance of a typical dictionary with an observable collection TValue.

OTHER TIPS

Can you not create the bindings in code? Making it more dynamic.

This MSDN page shows it possible via:

//make a new source
MyData myDataObject = new MyData(DateTime.Now);      
Binding myBinding = new Binding("MyDataProperty");
myBinding.Source = myDataObject;
myText.SetBinding(TextBlock.TextProperty, myBinding);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top