Mostrar un objeto C# complejo que tiene propiedades y una colección interna en WPF

Tengo una clase como esta:

public class NQWell : ObservableObject, ICloneable
    public string Name { get; set; }
    public ObservableCollection<KeyValuePair<int, double>> ResultsObservableCollection { get; set; }

ObservableCollection<NQWell> wells { get; set; }

I want to show this object in WPF (ListView, Grid, something..)

Debe tener un aspecto como este:


          Well   260    280  


           A1     0.2    0.3
           A2     0.1    0.4
           A3     0.6    0.5

The "Well" header comes from the name of the property, and the "260" and "280" are actually the keys from the observable collection.

The problem is, how do I normalize the object?

One possible solution that I thought would be to do it in itemscontrol for the first observable collection (wells) and show every property consequently in textblocks, and when it comes to the ObservableCollection> ResultsObservableCollection, I create another ItemsControl that walks that collection and shows it, and so on.

Is there a better way? My thoughts were, I do a ListView with GridView, and then create anonymous types for the outer properties, and another anonymous type for the inner observable collection, which I merge in a new object at the end. But this is somehow too much work. Has somebody any better propsals?

preguntado el 12 de junio de 14 a las 10:06

could you post some sample data? -

for ResultsObservableCollection you want to show just values or keys also? -

I've edited the question. The sample data is there. And yes, the keys come as part of the headers, and the values as part of the data. -

1 Respuestas

Your data object is not very complex at all... it just consist of a string name and a collection, so there should be no problem with displaying it. First, definir un DataTemplate for your class... clearly, you'll need a collection container and a TextBlock o similar:

<DataTemplate DataType="{YourPrefix:NQWell}">
            <RowDefinition Height="Auto" />
            <RowDefinition />
        <TextBlock Text="{Binding Name}" />
        <ListBox Grid.Row="1" ItemsSource="{Binding ResultsObservableCollection}" />

Como yo hice no especificar un x:Key valor para esto DataTemplate, it will be implicitly applied to all instances of your custom class. Now you've got a collection of these data types, so you'll need another container control:

<ListBox ItemsSource="{Binding wells}">
                <TextBlock Text="{Binding Key}" />
                <TextBlock Text="{Binding Value}" />

Now every item in this collection will be rendered using our DataTemplate, Incluyendo una Name Label y ListBox for it's items. The last part of this is to declare another DataTemplate para definir cuál es su KeyValuePair<int, double> items should look like. So as you can see, by breaking this problem down and then building this code up bit by bit, it is easy to accomplish.

Respondido el 12 de junio de 14 a las 11:06

Thank you for your answer. As I mentioned in the question, your answer has already been propsed by me as a solution. The thing is, I want to create a ListView. Another solution would be to use a datagrid with a dynamic dataset. - Mileski

Sorry, I thought that you were asking how to fulfil your solution requirements. In my personal opinion, this is a much better solution that trying to display this information into any rows and columns grid, because your data will not fit in them properly... it is not the correct shape for those controls. - Sheridan

You are right. I just wanted to see if somebody can offer maybe a more elegant solution. Take the word elegant as questionable. :) - Mileski

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.