Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'm in the process of rewriting a small application from console mode to a GUI mode.

The input is a file with log data from a measuring device:

...
00029;00044;00076;00044;00021
00309;00032;00037;00000;65535
00013;00016;00008;00007;00005
...

Each row of data represents 5 counters which summarize the range in which the measure fell, i.e. first column is "<40%", 2nd column is "[40-50[", and so on...

First row as example:

29 counts below 40% | 44 counts between 40% and 60% | 76 counts between 60% and 80%
44 counts between 80% and 90% | 21 counts between 90% and 100%

In the console application I used an array of integers to store each data row, putting each row array in a list. So far, so good... The application then used a for-loop to print out the values with descriptions to the console.

My problem: I wanted to create a GUI with WPF and apply databinding.

But I have a conceptual issue with how to create the data binding to the arrays of integers and visual elements.

I have not had problems with data binding to single properties. The challenge is due to the arrays.

I think I should create a class like this (could also be used as a ViewModel):

public class CounterRecords
{
    public List<CounterRecord> DataRecords { get; set; }
}

But how should I go about the CounterRecord class?
What's a clean / clear implementation of this object that I can bind to?

public class CounterRecord
{
    public int Range40 { get; set; }
    public int Range40_60 { get; set; }
    public int Range60_80 { get; set; }
    public int Range80_90 { get; set; }
    public int Range90_100 { get; set; }
}
share|improve this question
    
ObservableCollection? –  GalacticCowboy Feb 28 '13 at 14:53
    
Probably more appropriate on stackoverflow.com/questions you can ask the moderators to migrate this question. –  FrustratedWithFormsDesigner Feb 28 '13 at 15:02
1  
@GlenH7: Thank you for your edit! Love it how it looks now :o) –  DeepCore Mar 1 '13 at 6:39

1 Answer 1

up vote 2 down vote accepted

The Model layer should reflect your data, so if your log data represents 5 separate values which indicate a counter of a range, then that's what your Model should look like.

Your CounterRecord data model looks fine to me, however some things to take into consideration when using WPF's data binding systems are:

  • Are you going to be modifying the CounterRecord at runtime, and does this change need to be reflected in the UI? If so, you need to implement INotifyPropertyChanged on your CounterRecord class so it notifies the UI whenever it gets updated

  • Are you going to be modifying the collection of CounterRecord objects at runtime, and does this change need to be reflected in the UI? If so, you will want to use an ObservableCollection instead of a List<T> for the same reasons

To bind to a collection instead of a single property, you'll want a UI control with an ItemsSource property, such as a DataGrid or an ItemsControl

<DataGrid ItemsSource="{Binding DataRecords}" />

or

<ItemsControl ItemsSource="{Binding DataRecords}" />

Basically, an ItemsSource binding will loop through your collection and create a UI element for each item in the collection. That UI element may be a DataGridRow, a TabItem, a ListBoxItem, or even some custom element that you define with an ItemTemplate.

If you're interested, my blog has a better explanation of an ItemsControl than MSDN has, and it will probably help you understand the binding process to collections a bit better.

share|improve this answer
    
Thank you for the link to your blog! It helped me get a grasp at the ItemsControl. Neither the CounterRecord nor the collection of it will be modifed during runtime because the application is used to display a single logfile at a time. –  DeepCore Mar 1 '13 at 6:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.