Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a good way to display dynamic data in WPF?

I am struggling with the best way to handle a WPF application that has dynamic data updates. Should I use the rich data binding provided by WPF or not?

I will simplify my problem domain as much as possible in the discussion below, and briefly mention some solutions. I am very interested in whatever comments this community can provide.

Simplified Problem Domain: Suppose you have a system with about 1,000 items, each with a unique “name”, and a dynamic “speed” and “location”. The data for these items is updated 60x per second. You wish to display this data to the user in a variety of UI’s – everything from “labels displaying numbers” to graphical dials/meters/gauges/etc. Perfect job for WPF – separate the visuals from the rest of the application. How would you design this system?

My first solution: My first solution was to define an object with speed and location properties (doubles) called “DataItem”, define an interface with a name property (string). Controls that display the data would have to implement the name interface, A “DataManager” was created that scanned for FrameworkElements that implemented the interface, built a list a name/FrameworkElements pairs at initialization time. Then, 60 times per second, the list of 1,000 DataItems was updated, and the DataContext of each matching FrameworkElement was set to the DataItem object. This worked, performance was acceptable (particularly if data was not always changing).

My second solution addressed the problem that the UI Controls in the first solution all had to implement some interface – yuk. I want to use out-of-the-box-unmodified WPF controls (in some cases). So, the second solution was to define an “Attached property” (I put it on the DataManager object), so you could – in xaml – do stuff like

<Label DataManager.Name = "objectname"  Content="{Binding}" />

Somehow, this solution still does not seem right. My third solution was to look into implementing a custom DataSourceProvider. I was not successful. I could not get my head around the data source provider model, and how it would be used in this case.

Right now, I am looking at the CodePlex “dynamic data display” project posted by Microsoft Research. That project is all about plotting/graphing dynamic data, but there could be some ideas there. So here I am on StackOverflow – normally a place with short questions and quick answers. :-)

I am very new to WPF and would appreciate any thoughts anyone has on these issues. Please keep in mind that the problem domain specified here is simplified, so I need a robust solution. There are actually many different types of data objects, each with different properties; and data updates are not the same rate for each object, and the UI controls display single items and groups of items, the data comes in from many sources, etc.

like image 972
Bill Avatar asked Dec 13 '08 16:12

Bill


1 Answers

You're almost there. You want to data bind at two levels.

First - have your DataItems implement INotifyPropertyChanged or have them derive from DispatchingObject and implement dependency properties for each of the values you want to bind to

Second - keep all of your DataItems in one of ObservableCollection<DataItem> or a DataTable. You need a collection that supports INotifyCollectionChanged or IBindingList.

Third - use some kind of container control (listbox, 3rd pary grid control, etc) to bind the list of DataItems to your app

Fourth - Define a DataTemplate to give each of the DataItems a visual look

Fifth - if needed, use a DataTemplateSelector to dynamically choose the DataTemplate for different kinds of DataItems

Here's a minimal bit of xaml to get you started

<ListBox ItemsSource="{Binding ...}" ItemTemplateSelector="{StaticResource DTSelector}">
like image 111
Scott Weinstein Avatar answered Sep 28 '22 05:09

Scott Weinstein