Reputation: 27
I need to bind a textblock.text
property to a single element in an observable collection, or array element, and have the text update using the INotifyPropertyChanged
or INotifyCollectionChanged
, whichever is best.
Most examples describe ways to bind listboxes or other list views to entire collections, but my application needs to update several textblocks on a screen depending on notification of a change in one or more elements of an array.
textblock1.Text = MyArray(0)...
textblock2.Text = MyArray(1)...
textblock3.Text = MyArray(2)...
textblock4.Text = MyArray(3)...
etc...
Is it possible to bind a single textblock to a single array element?
Is it possible to get notification of the proper type that will update one or more of the textblocks if any assigned element changes?
Upvotes: 0
Views: 2625
Reputation: 10823
All things are possible in WPF, one way or the other (or, usually, both, plus a bunch more).
The easy part first - if you've properly implemented INotifyPropertyChanged on the object in your array, bindings should update properly. INotifyCollectionChanged notifies you if elements in a collection have changed (i.e. been added/deleted).
It sounds like you are trying to update an unknown number (or even a known number, it doesn't really matter) of TextBlocks. Likely, the best way to do that is to use some kind of ItemsControl (ListBox is one) with an ItemsTemplate, and optionally the ItemsPanel. This will be the easiest way to maintain, in case the definition of the collection changes.
For instance, here is one ItemsControl example.
<ItemsControl x:Name="itemsExample"
ItemsSource="{Binding MyCollection}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel HorizontalAlignment="Stretch" IsItemsHost="True" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding SomeStringProperty}" Grid.Column="0" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
If, however, you really do want to bind individual TextBlocks, one way you can do it by implementing IValueConverter. You would then bind each of your TextBlocks to the collection, and use a ConverterParameter with the appropriate index. The converter would then just return the value of a string at that index.
<TextBlock Text="{Binding MyCollection,
Converter={StaticResource myObjectConverter},
ConverterParameter=0}" />
If you are using MVVM, another possibility is to have properties for each of the elements of your array, and bind to those properties. However, if you are doing that, I would question the need for the array in the first place.
Upvotes: 2