【问题标题】:How to pass current node object to to command in telerik RadtreeviewItem in wpf如何将当前节点对象传递给wpf中telerik RadtreeviewItem中的命令
【发布时间】:2019-05-22 10:15:49
【问题描述】:

我在 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...

【问题讨论】:

  • 你的意思是树视图项的数据上下文?绑定datacontext relativesource self
  • @Andy 我是 wpf 的新手,以及 wpf 的 Telerik .. 我可以知道如何将当前项目传递给命令参数
  • @Andy 我可以知道我们如何传递参数

标签: wpf events telerik radtreeview custom-button


【解决方案1】:

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

DataContext 沿可视化树继承。

因此按钮的数据上下文是树视图项的。

你在哪里

 CommandParameter="{Binding ElementName=radTreeView}"

你想要按钮所在的数据上下文。应该是这样的:

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

再想一想,也有可能

CommandParameter="{Binding}"

【讨论】:

  • 谢谢...CommandParameter="{Binding DataContext, RelativeSource={RelativeSource Self}}"CommandParameter="{Binding}" 都给了我想要的输出
猜你喜欢
  • 2019-10-04
  • 2018-10-03
  • 1970-01-01
  • 1970-01-01
  • 2013-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多