views:

1213

answers:

2

I have a collection of ViewModels bound to a ListBox. I am trying to bind the IsSelected properties of each together. 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 accomplish this?

+1  A: 

In Silverlight, you are not able to create "global" styles, that is, styles that modify all controls of a certain type. Your style needs a key, and your control needs to reference it.

Also, TargetType simply needs the control type name. Silverlight does not support the x:Type extension.

ib.

Ireney Berezniak
A: 

Here's 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 relative binding, which doesn't exist in Silverlight unfortunately... BindingHelper is a helper class which overcomes this limitation (search for "relative binding in silverlight" to find it).