开发者

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 Silve开发者_如何学编程rlight 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?


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).


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)?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜