0

I have an public IEnumerable<ISlot> enumSlots;.
Which is declared in my MainWindow and filled with values:

public MainWindow()
{
   InitializeComponent();

   enumSlots = GetSlots();
   this.DataContext = this;
}

The ISlot:

public interface ISlot
{
   int Number { get; }
   string Name { get; }
}

I have a custom control:

  <Style Selector="controls|CustomControlSlot">
    <Setter Property="Template">
      <ControlTemplate>
        <StackPanel Orientation="Vertical">
         <Label x:Name="labelNumber" Content="Number"/>
         <Label x:Name="labelName" Content="Name"/>
        </StackPanel>
      </ControlTemplate>
    </Setter>
  </Style>
</Styles>

My goal is, that for each item in the IEnumerable a custom control with the filled labels is displayed in in MainWindow.

SideSky
  • 313
  • 3
  • 15
  • 2
    `enumSlots` must be a public property, not a field. You would then bind the ItemsSource property of an ItemsControl to the `enumSlots` property. The ItemTemplate would contain a DataTemplate with the two Labels, or better TextBlocks.. A custom control is not necessary. Start reading here: [Data Templating Overview](https://learn.microsoft.com/en-us/dotnet/desktop/wpf/data/data-templating-overview?view=netframeworkdesktop-4.8). – Clemens Dec 06 '21 at 06:53
  • 2
    Also be aware that the interface ISlot is not required. You could as well bind an `IEnumerable`. It is only important that the objects in the collection provide public Number and Name properties. The binding system finds them by reflection. – Clemens Dec 06 '21 at 06:56

0 Answers0