Malcolm O'Hare
Malcolm O'Hare

Reputation: 4999

Silverlight Treeview SelectedItem TwoWay binding causing error in blend

I have a Treeview in a Silverlight 4 project, and I want to bind to its SelectedItem. When I do a binding to SelectedItem (Mode=TwoWay) its throwing an error in blend because SelectedItem is readonly, which is causing my XAML to not render. I don't ever want to SET the SelectedItem property, I just want to know when it changes via UI interaction. In WPF, I would just bind its SelectedItem using Mode=OneWayToSource, but Silverlight does not support that mode (afaik).

Treeview :

<controls:TreeView ItemsSource="{Binding Repository.MajorClasses}" SelectedItem="{Binding SelectedItem, Mode=TwoWay}" />

Is there a workaround that anyone has used? And anyone know why OneWayToSource is omitted from Silverlight?

Upvotes: 1

Views: 897

Answers (3)

Izmoto
Izmoto

Reputation: 1959

What you need to do is make use of an Interaction Trigger and bind it to the SelectedItemChangedevent as follows:

  <sdk:TreeView x:Name="ModuleNavigationItemWrappersTreeView" ItemsSource="{Binding ModuleNavigationItemWrappers}">
                        <sdk:TreeView.ItemTemplate>
                            <sdk:HierarchicalDataTemplate ItemsSource="{Binding Children}">
                                <StackPanel Orientation="Horizontal" Margin="0,2,0,2">
                                    <Image Source="/VanguardFinancials.Common;component/Images/icons/flag_blue.png" />
                                    <TextBlock Margin="2,0,0,0" Text="{Binding ItemDescription}"></TextBlock>
                                </StackPanel>
                            </sdk:HierarchicalDataTemplate>
                        </sdk:TreeView.ItemTemplate>
                        <interactivity:Interaction.Triggers>
                            <interactivity:EventTrigger EventName="SelectedItemChanged">
                                <interactivity:InvokeCommandAction Command="{Binding TrackSelectedModuleNavigationItemWrapper}" CommandParameter="{Binding ElementName=ModuleNavigationItemWrappersTreeView}" />
                            </interactivity:EventTrigger>
                        </interactivity:Interaction.Triggers>
                    </sdk:TreeView>

Visit this for more information about Behaviors and Triggers. Hope this helps.

Upvotes: 0

A Aiston
A Aiston

Reputation: 717

If you just want your VM to be informed when the user changes the selection, you should be able to do exactly what you are doing (a two way binding).

I have this working in Visual studio so, I suggest trying it from there, might just be a problem with Blend. VS intellisense doesn't suggest SelectedItem when typing in the XAML editor but that doesn't stop it from working.

The bound property in your VM is definately of the right type (MajorClass by the looks of it)?

Upvotes: 0

Vladimir Dorokhov
Vladimir Dorokhov

Reputation: 3839

It's really readonly, so you cann't do that. You can use TreeView as base control and create CustomTreeView with implementation of bindable SelectedItem. Or create own behavior(attached property). Or use some third party control (f.i. telerik).

Upvotes: 1

Related Questions