如何将当前节点对象传递给 wpf 中的 telerik RadtreeviewItem 命令

How to pass current node object to to command in telerik RadtreeviewItem in wpf

我在 WPF 中使用 Kendo Telerik RadTreeview 控件。我具有为每个 RadTreeViewItem 节点添加一个自定义按钮的功能,该按钮在命令事件显示时弹出。

我添加了按钮并在 HierarchicalDataTemplate 中使用它,如下所示

<HierarchicalDataTemplate x:Key="BuildingStructure"
                              ItemsSource="{Binding Levels, Mode=TwoWay}"
                              ItemContainerStyle="{StaticResource levelNodeStyle}">
        <Grid HorizontalAlignment="Stretch">
            <Grid.ColumnDefinitions>
                <ColumnDefinition  Width="3*"/>
                <ColumnDefinition  Width="*"/>
            </Grid.ColumnDefinitions>
            <TextBlock Grid.Column="0" 
                       Width="250"
                       Text="{Binding StructureName , Mode=TwoWay}" 
                       HorizontalAlignment="Left"
                       Panel.ZIndex="2"/>
            <Button
                Canvas.Left="10" 
                Canvas.Bottom="20"
                Panel.ZIndex="1"
                BorderThickness="0"
                BorderBrush="Transparent"
                Background="Transparent"
                 Foreground="White"
                HorizontalAlignment="Left"
                Grid.Column="1"
                VerticalAlignment="Stretch"
                Command="{Binding DataContext.AddLevelRadTreeCommand, RelativeSource={RelativeSource AncestorType=telerik:RadTreeView}}" 
                CommandParameter="{Binding ElementName=radTreeView}"
                Margin="0 2 0 5">

                <Image
                    Width="20"
                    Height="20"
                   Source="/Project;component/Resources/Images/03-Add.png"/>
            </Button>
        </Grid>


    </HierarchicalDataTemplate>

我想要的是在命令事件中我需要添加命令参数,该参数将传递当前 RadTreeviewItem 分配给该节点的数据对象,如下所示

StructId:1,
StructName:'Building A'....and so on...

当您将数据模板化到控件中时,控件的数据上下文将成为您正在模板化的视图模型。 Levels 中的每一个都成为生成的 treewview 项目的数据上下文。

DataContext 沿可视化树向下继承。

因此按钮的数据上下文是 treeviewitem 的。

你在哪里

 CommandParameter="{Binding ElementName=radTreeView}"

您需要按钮所在的数据上下文。应该类似于:

CommandParameter="{Binding DataContext, RelativeSource={RelativeSource Self}}"

仔细想想,也可以

CommandParameter="{Binding}"