0

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.

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Don't forget to use an observable collection to store your strings.
Oh so not an array then, something like a List<String>? And I guess I just use this ItemsControl and have a <Label> for every string in the source?
You can use a List<string> or ObservableCollection<string>, but the OC will give you collection change notifications, so the UI will update when the collection changes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.