Kornelije Petak
Kornelije Petak

Reputation: 9572

Reapplying template for ListBox item programatically

Basically, I have a list of colors and a defined datatemplate for listbox item:

<DataTemplate x:Key="colorItemDataTemplate">
    <Border x:Name="borderInner" BorderBrush="Black" BorderThickness="1" Background="{Binding Brush}" Width="11" Height="11" />
</DataTemplate>

Now, when I add a bunch of items into the listbox and then set the ListBox.ItemsSource property to my List, the listbox is filled correctly.

There is also a slider with its appropriate event handler. Within the event handler, Brush property of one of the items from the listbox is changed. Since the appearance of the item depends on the Brush Property, listbox should reflect the change.

I could reset the ItemsSource property, but then all items have their templates applied and with more than 200 items in the listbox, this is pretty slow.

So, is there any way to refresh the template for only one item from the listbox?

Thanx

Upvotes: 0

Views: 1145

Answers (2)

Kent Boogaart
Kent Boogaart

Reputation: 178700

I'm not sure I follow. If you've bound the Background to the property, changing the property should automatically udpate the background of the ListBoxItem. If you're not seeing that, make sure you are either using a DependencyProperty or implementing INotifyPropertyChanged.

Upvotes: 1

Jacob Adams
Jacob Adams

Reputation: 3994

You could use a binding converter. In the converter class you could have some logic like

(pseudo-code)
if (ListBoxItem.IsSelected)
   return SpecialColorFromSlider
else
   return NormalListBoxColor

Upvotes: 0

Related Questions