views:

102

answers:

1

I am using MVVM pattern with Silverlight 4 to bind a collection of TODO items to a ListBox.

There is a property IsSelected on each TODO entity. This allow for multiple selections to be made in the UI that are bound back to the ViewModel. At the same time any changes made by the ViewModel get reflected in the View.

I am basically trying to do what was suggested in this answer.

Unfortunately in Silverlight (as opposed to WPF) I just cannot find a way to do this with the template since Bindings in a Style Setter are not supported in SL4.

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

However unlike any other method - this seems to be the most reliable for two way binding of a selected items list.

How can I express this binding in codebehind or XAML?

+2  A: 

The easiest way I know of is to overload the DataTemplate and make it look like a ListBoxItem selection. I used a DataStateBehavior from the Blend 4.0 Silverlight SDK to tie the IsSelected property to the correct look and feel.

<i:Interaction.Behaviors>
    <ei:DataStateBehavior Binding="{Binding IsSelected, Mode=TwoWay}" Value="True" TrueState="Selected" FalseState="Unselected"/>                   
</i:Interaction.Behaviors>

Rather than paste the entire thing, I'm linking to a post I added to my blog here.

WPCoder