【问题标题】:Silverlight TreeViewItem, Binding to IsSelected, how to?Silverlight TreeViewItem,绑定到 IsSelected,如何?
【发布时间】:2011-05-05 15:12:49
【问题描述】:

在阅读有关 TreeView 和绑定到视图模型的帖子 (http://www.codeproject.com/KB/WPF/TreeViewWithViewModel.aspx) 时,似乎可以绑定 TreeViewItem IsSelected 属性。但是,我有以下代码在 Initialize() 上总是失败,因为它试图设置只读属性?

       <sdk:TreeView Grid.Column="0" Grid.Row="2" Style="{StaticResource TreeViewStyle}" 
                  ItemsSource="{Binding tvData}" > 
            <sdk:TreeView.ItemContainerStyle> 
                <Style TargetType="sdk:TreeViewItem"> 
                    <Setter Property="IsEnabled" Value="True" /> 
                    <Setter Property="IsExpanded" Value="True" /> 
                    <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" /> 
                </Style> 
            </sdk:TreeView.ItemContainerStyle> 
            <sdk:TreeView.ItemTemplate> 
                <sdk:HierarchicalDataTemplate ItemsSource="{Binding Children}"> 
                    <StackPanel Orientation="Horizontal"> 
                        <TextBlock Text="{Binding ItemName}" FontWeight="{Binding ItemFontWeight}"/> 
                    </StackPanel>                         
                </sdk:HierarchicalDataTemplate>                     
            </sdk:TreeView.ItemTemplate>

【问题讨论】:

    标签: silverlight-4.0 treeview mvvm-light


    【解决方案1】:

    我可以推荐这种技术来解决这个问题:

    http://blogs.msdn.com/b/delay/archive/2009/05/07/one-more-platform-difference-more-or-less-tamed-settervaluebindinghelper-makes-silverlight-setters-better.aspx

    编辑: 我应该提一下,我还没有尝试过这种确切场景的技术(绑定 TreeViewItem 的 IsSelected 属性),但我已经在许多其他场合使用过它,到目前为止它工作得完美无缺。

    【讨论】:

      【解决方案2】:

      您不能通过SetterStyle 中分配绑定。实际上,您正在尝试在 Setter.Value 属性上设置绑定。 Xaml 不会推断您要在目标属性上设置绑定。反过来,Setter 只是假设您正在尝试将一个值直接设置为IsSelected,它知道它是只读的,因此会出现错误。

      【讨论】:

        猜你喜欢
        • 2014-04-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-19
        • 2011-04-06
        • 2011-12-14
        相关资源
        最近更新 更多