Silverlight MVVM ListBoxItem IsSelected

I have a ViewModels collection related to a ListBox. I am trying to bind the IsSelected properties of each of them. In WPF, it works by setting the style:

<Style TargetType="{x:Type ListBoxItem}"> <Setter Property="IsSelected" Value="{Binding Path=IsSelected, Mode=TwoWay}" /> </Style> 

This does not work in Silverlight. How can i do this?

+4
source share
2 answers

In Silverlight, you cannot create β€œglobal” styles, that is, styles that change all controls of a particular type. Your style needs a key, and your control should reference it.

In addition, TargetType just needs a control type name. Silverlight does not support the x: Type extension.

I.B ..

+2
source

Here is how I do it:

 <ListBox.ItemTemplate> <DataTemplate> ... <CheckBox VerticalAlignment="Top" HorizontalAlignment="Left" x:Name="CheckBox1" IsChecked="True" Grid.Row="0"> <inf:BindingHelper.Binding> <inf:BindingProperties TargetProperty="Visibility" SourceProperty="IsSelected" Converter="{StaticResource VisibilityConverter}" RelativeSourceAncestorType="ListBoxItem" /> </inf:BindingHelper.Binding> </CheckBox> ... </DataTemplate> </ListBox.ItemTemplate> 

You need to do a relative binding, which, unfortunately, is not in Silverlight ... BindingHelper is a helper class that overcomes this restriction (search for "relative binding in silverlight" to find it).

+1
source

All Articles