TreeView - 绑定到IsExpanded属性

时间:2014-02-25 20:36:59

标签: c# wpf treeview mvvm-light

我有一个声明如下的TreeView:

<TreeView Grid.Column="0" HorizontalAlignment="Stretch" DockPanel.Dock="Left" ItemsSource="{Binding Path=SomeBinding, UpdateSourceTrigger=PropertyChanged}">
    <TreeView.ItemContainerStyle>
        <Style TargetType="{x:Type TreeViewItem}">
            <Setter Property="IsExpanded" Value="{Binding Path=DataContext.DoSmth, UpdateSourceTrigger=PropertyChanged, 
                                                            RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TreeView}}}"/>
        </Style>
    </TreeView.ItemContainerStyle>
    <TreeView.ItemTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding SomeOtherBinding}">
            <TextBlock Text="{Binding}" />
        </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
</TreeView>

不幸的是,这种绑定不起作用,并且没有调用DoSmth。

修改

我添加了此代码,但它也无效:

<i:Interaction.Triggers>
    <i:EventTrigger EventName="TreeViewItem.Expanded">
        <i:InvokeCommandAction Command="{Binding Path=DataContext.DoSmth}" CommandParameter="{Binding SelectedItem, RelativeSource={RelativeSource AncestorType={x:Type TreeView}}}" />
    </i:EventTrigger>
</i:Interaction.Triggers>

修改

我想绑定到TreeViewItem,我认为我在两个代码段都这样做了。如果任何这些项目将被扩展,我也希望能够运行DoSmth命令

0 个答案:

没有答案