在展开时获取TreeViewItem的绑定对象

时间:2017-08-09 08:31:04

标签: c# wpf

我得到了这个TreeView

<TreeView x:Name="treeViewSL" FontSize="14" Margin="6,65,318,4" TreeViewItem.Selected="TreeViewItem_Selected" TreeViewItem.Expanded="TreeViewItem_Expanded"  Grid.RowSpan="3" SnapsToDevicePixels="True" >
    <TreeView.ItemTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding ChildNodes}">
            <StackPanel Orientation="Horizontal" >
                <TextBlock Name="tbDoc" FontFamily="{StaticResource FontAwesome}" Text="&#xe022;"  Padding="2" Style="{StaticResource tbDocStyle}" Visibility="{Binding HasDocs, Converter={StaticResource VisibilityConverter}}" />
                <TextBlock Name="tbPart" FontFamily="{StaticResource FontAwesome}" Text="&#xe006;" Style="{StaticResource tbPartStyle}" Padding="2" Visibility="{Binding ET, Converter={StaticResource VisibilityConverter}}" />
                <TextBlock Text="{Binding NodeText}" ></TextBlock>
            </StackPanel>
        </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
    <TreeView.Resources>
        <Style TargetType="{x:Type TreeViewItem}" BasedOn="{StaticResource TreeViewItemStyle}"/>
        <Style TargetType="ToolTip" BasedOn="{StaticResource {x:Type ToolTip}}">
            <Setter Property="Foreground" Value="White"></Setter>
            <Setter Property="Background" Value="#FF5DD5F0"></Setter>
            <Setter Property="BorderBrush" Value="#FF3266A0"></Setter>
            <Setter Property="Height" Value="40"></Setter>
        </Style>
    </TreeView.Resources>
</TreeView>

我使用LazyLoad以便在扩展时加载项目,我已经得到了这样的函数:

private void TreeViewItem_Expanded(object sender, RoutedEventArgs e)
{
    var senderItem = e.OriginalSource as ArtikelTreeNode;
}

我想要的是把这个项目作为绑定对象给我,现在这只给我null了, 我已经尝试过:treeviewsl.Selecteditem as ArtikelTreeNode,这样可行,但方法Expanded在选中之前被触发,所以我总是得到最后一个节点......

我如何取回物品作为物品绑定?

1 个答案:

答案 0 :(得分:1)

试试这个:

private void TreeViewItem_Expanded(object sender, RoutedEventArgs e)
{
    TreeViewItem tvi = e.OriginalSource as TreeViewItem;
    var senderItem = tvi.DataContext as ArtikelTreeNode;
}