문제

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

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top