【问题标题】:How to add command event and pass the current node data in wpf telerik RadTreeviewItem with custom button如何使用自定义按钮添加命令事件并在 wpf telerik RadTreeviewItem 中传递当前节点数据
【发布时间】:2019-10-04 18:41:12
【问题描述】:

我在 WPF 中使用 Kendo Telerik RadTreeView 控件。我有每个 RadTreeViewItem 的功能,我需要显示一个按钮,我可以这样做。

现在每个RadTreeViewItem旁边的按钮都会有事件/命令,需要打开弹出窗口并传递当前节点数据。

我已经尝试使用按钮单击事件背后的代码,它工作正常。但根据要求,我们应该只使用命令而不是事件背后的代码。

这是我添加按钮的代码..

<HierarchicalDataTemplate x:Key="BuildingStructure"
                              ItemsSource="{Binding StructureLevels}"
                              ItemContainerStyle="{StaticResource levelNodeStyle}">

        <Grid HorizontalAlignment="Stretch">
            <Grid.ColumnDefinitions>
                <ColumnDefinition  Width="3*"/>
                <ColumnDefinition  Width="*"/>
            </Grid.ColumnDefinitions>
            <TextBlock Grid.Column="0" 
                       Width="250"
                       Text="{Binding Name}" 
                       HorizontalAlignment="Left"/>
            <Button
                Panel.ZIndex="100"
                BorderThickness="0"
                BorderBrush="Transparent"
                Background="Black"
                Grid.Column="1"
                VerticalAlignment="Stretch"
                Margin="0 2 0 5">
                <Image
                    Width="20"
                    Height="20"
                   Source="/Project;component/Resources/Images/03-Add.png"/>

                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="MouseLeftButtonDown">
                        <i:InvokeCommandAction Command="{Binding TestRadTreeCommand}"/>
                    </i:EventTrigger>
                </i:Interaction.Triggers>

            </Button>
        </Grid>
    </HierarchicalDataTemplate>

请查看图片以供参考:

提前谢谢...!!

【问题讨论】:

  • Button 具有Command 属性,因此没有理由使用EventTriggerInvokeCommandAction 来处理MouseLeftButtonDown 事件。 TestRadTreeCommand 是在哪里定义的?
  • TestRadTreeCommand 在 ViewModel 中定义,我尝试使用 Command 属性但它没有被调用

标签: wpf events telerik radtreeview custom-button


【解决方案1】:

Button 具有Command 属性,因此没有理由使用EventTriggerInvokeCommandAction 来处理MouseLeftButtonDown 事件。

如果在视图模型中定义了TestRadTreeCommand,即TreeViewDataContext,您可以像这样绑定到它:

<Button
    Panel.ZIndex="100"
    BorderThickness="0"
    BorderBrush="Transparent"
    Background="Black"
    Grid.Column="1"
    VerticalAlignment="Stretch"
    Margin="0 2 0 5"
    Command="{Binding DataContext.TestRadTreeCommand, RelativeSource={RelativeSource AncestorType=TreeView}}">
    <Image Width="20"
           Height="20"
           Source="/CCRIS;component/Resources/Images/03-Add.png"/>
</Button>

【讨论】:

  • 如您所见,我正在添加命令,我需要为其添加参数.. 参数应包含该节点的数据.. 我该如何实现。提前致谢
  • 这与您的原始问题有什么关系?如果您有其他问题,请提出一个新问题。不要在 cmets 字段中提出其他问题。
  • 好吧,我会的,但有时一切都朝着一个方向发展,中间有一些东西............
  • 是的,我得到了想要的输出
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-02
  • 1970-01-01
  • 2010-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-11
相关资源
最近更新 更多