简体   繁体   English

在TreeView上使用ItemTemplate时如何绑定样式中的IsSelected

[英]How to databind IsSelected in Style when using ItemTemplate on TreeView

I'm attempting to make a TreeView which uses an ItemTemplate , and also is bound to a view model which has a IsSelected property. 我试图制作一个使用ItemTemplateTreeView ,并且还绑定到具有IsSelected属性的视图模型。

I'm not exactly sure why this isn't working, so I apologize if the question seems vague. 我不确定这为什么行不通,因此,如果问题不清楚,我深表歉意。

I've also reduced the code to what I think is the problem, as my app has a lot more XAML , so I hope what I've included is sufficient. 我还将代码简化为我认为的问题,因为我的应用程序具有更多的XAML ,所以我希望所包含的内容足够。

Here's how I define my DataTemplate: 这是我定义DataTemplate的方法:

    <DataTemplate x:Key="ElementDataTemplate">
        <Label Content="{Binding Path=DisplayText}"></Label>
    </DataTemplate>

Here's how I use it: 这是我的用法:

     <TreeView ItemsSource="{Binding Elements}" 
               ItemTemplate="{DynamicResource ElementDataTemplate}">
               <TreeView.Resources>
                   <Style TargetType="ListBoxItem">
                      <Setter Property="IsSelected"
                              Value="{Binding Path=IsSelected, Mode=TwoWay}" />
                      </Style>
                </TreeView.Resources>
      </TreeView>

My ViewModel has DisplayText and IsSelected properties. 我的ViewModel具有DisplayText和IsSelected属性。

I know that I'm binding correctly to the ViewModel because I am seeing the same number of items as in my Elements property of my data context, and my Label's Coutent is properly being set to DisplayText- I've verified this through breakpoints on the getter and visually when I run the app. 我知道我已正确绑定到ViewModel,因为我看到的项目数量与数据上下文的Elements属性中的项目数量相同,并且Label的Coutent已正确设置为DisplayText-我已经通过断点验证了这一点当我运行该应用程序时,会发生吸气和视觉变化。

However, the IsSelected property's getter on the ViewModel never gets called (never hits a breakpoint), so it's clear I've messed up something on the binding of the IsSelected property. 但是,ViewModel上的IsSelected属性的getter永远不会被调用(永远不会遇到断点),因此很明显,我在IsSelected属性的绑定上搞砸了。

I should point out that when I run the application I can select items with the mouse and they reflect the selection visually, so the TreeView item itself is getting selected, it's just not binding to the ViewModel's IsSelected property. 我应该指出,当我运行应用程序时,我可以使用鼠标选择项目,并且它们可以直观地反映选择,因此TreeView项本身已被选中,它只是不绑定到ViewModel的IsSelected属性。 Any help would be much appreciated! 任何帮助将非常感激!

Ahh, I'm foolish! 啊,我真傻! I spent forever trying to figure out what it was, came here after a few hours, then finally figured it out: 我花了很长时间试图弄清楚它是什么,几个小时后才来到这里,然后终于弄明白了:

  <TreeView.ItemContainerStyle>
      <Style TargetType="TreeViewItem">
          <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}"/>
      </Style>
  </TreeView.ItemContainerStyle>

I was using "ListBoxItem" instead of "TreeViewItem". 我使用的是“ ListBoxItem”而不是“ TreeViewItem”。 Copy/paste got the best of me. 复制/粘贴得到了我最好的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM