0

I want WPF read C# Object's properties. And convert these properties name to WPF's Label controls.

1 Answers1

1

The StackPanel does NOT generate items. It's only a Panel, whose function is Layout only.

You're looking for an ItemsControl:

<ItemsControl ItemsSource="{Binding}">
   <!-- ... -->
</ItemsControl>

which, by default will have a StackPanel as it's ItemsPanel.

Notice also that setting the DataContext to a single instance of a class will NOT make the ItemsControl create any elements. You need to set the ItemsSource property to an IEnumerable (for example a List<MyClass> or the like).

//Window Constructor
public MainWindow()
{
    DataContext = New List<MyClass>
                  {
                      //.. Items here
                  };
}

And no, WPF does not automatically read Attributes from properties. You can create a ViewModel which does that, or hard-code the display names in XAML.

Federico Berasategui
  • 43,562
  • 11
  • 100
  • 154
  • +1 Nice to see you answering questions again after your commenting period. :) – Sheridan Jan 15 '14 at 15:47
  • @sheridan I keep waiting for [these](http://stackoverflow.com/a/21004225/643085) *"Ok, Delete all your code and start all over..."* kind of situations... :P – Federico Berasategui Jan 15 '14 at 15:49
  • You shouldn't have to wait long... every other mother here claims to be using WPF and MVVM and half of them aren't using either!! – Sheridan Jan 15 '14 at 15:56