Ternary
Ternary

Reputation: 2411

Data Binding Label Content to Array

I am somewhat new to WPF and Data Binding, it seems very powerful. I'm wondering if there's a way to have a set of labels, and have there Content property all binded to a different index in array of strings. So then as the array is updated, the labels automatically change too.

The xaml syntax is still a bit foreign to me and I haven't been able to get it to work.

Upvotes: 0

Views: 1416

Answers (1)

devdigital
devdigital

Reputation: 34349

If this is a dynamic set of labels, then you may be better off using an ItemsControl, and changing its ItemTemplate to display a label for each item in the collection that it is bound to (a collection of strings in your case).

Something like:

<ItemsControl ItemsSource="{Binding MyLabelStrings}">
  <ItemsControl.ItemTemplate>
    <DataTemplate>
      <Label Content="{Binding}" ... />
    </DataTemplate>
  </ItemsControl.ItemTemplate>
</ItemsControl>

As Bojin mentions, if you wish your UI to update if strings are added/removed from the collection, then use an ObservableCollection for the MyLabelStrings property.

Upvotes: 4

Related Questions