ItemsControl 内按钮的 WPF 命令参数 [英] WPF Command Parameter for button inside ItemsControl

查看:45
本文介绍了ItemsControl 内按钮的 WPF 命令参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 MVVM 设计模式开发 WPF 应用程序.在应用程序中,我有一个 ItemsControl 显示几个按钮(根据应用程序的逻辑流程,按钮的数量不是恒定的).ItemsControl 绑定到 ViewModel 中的 LinkedList.我能够为按钮定义一个命令,并且只有当命令没有参数时,我才能在 ViewModel 中获取命令处理程序......我需要能够知道点击了哪个按钮,因此我想要一个带参数的命令.

I am developing a WPF application using the MVVM design pattern. In the application I have an ItemsControl that display several Buttons (number of buttons is not constant, according the logic flow of the application). The ItemsControl is binded to LinkedList in the ViewModel. I am able to define a Command for the Buttons and I able to get the Command handler within the ViewModel only if the Command has no parameters... I need to be able to know which button has been clicked and therefore I would like to have a Command with Parameter.

我需要如何定义我的 View ?CommandParameter 的语法是什么,我可以使用哪个参数来获取 Button 内容?ViewModel 中的 Command 签名是什么?

How do I need to define my View ? What is the Syntax for the CommandParameter and which parameter I can used to get the Button content ? What will be the Command signature in the ViewModel ?

请注意,我使用的是来自 MVVM Light 的 RelayCommand.视图的相关代码在这里.提前致谢

Please note that I am using RelayCommand from MVVM Light. The relvant code of the View is here. Thanks in advance

<UserControl x:Class="Museum.Controls.CategoriesView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="130" d:DesignWidth="418">
    <UserControl.Resources>
        <DataTemplate x:Key="CategoriesButtonsTemplate">
            <Button Content="{Binding }" 
                    Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}, Path=DataContext.CategorySelectedCommand}" 
                    />
        </DataTemplate>
    </UserControl.Resources>
    <Grid ShowGridLines="True">
        <Grid.RowDefinitions>
            <RowDefinition Height="50"></RowDefinition>
            <RowDefinition Height=" 40"></RowDefinition>
            <RowDefinition Height=" 40"></RowDefinition>
        </Grid.RowDefinitions>
        <Grid x:Name="SubSubjectGrid" Grid.Row="0"></Grid>
        <Grid x:Name="CategoriesGrid" Grid.Row="1">
            <ItemsControl Grid.Row="1" 
                  ItemsSource="{Binding Path=CategoriesButtons}" 
                  ItemTemplate="{StaticResource CategoriesButtonsTemplate}" >
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel Orientation="Horizontal"  HorizontalAlignment="Stretch"/>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
            </ItemsControl>
     </Grid>
        <Grid x:Name="SelectedCategoryGrid" Grid.Row="2"></Grid>
    </Grid>  
</UserControl>

推荐答案

你可以像这样简单地使用 CommandParameter="{Binding }"

You can simply use CommandParameter="{Binding }" like this

       <DataTemplate x:Key="CategoriesButtonsTemplate">
        <Button Content="{Binding }" 
                Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}, Path=DataContext.CategorySelectedCommand}" 
                CommandParameter="{Binding }" 
                />
    </DataTemplate>

这篇关于ItemsControl 内按钮的 WPF 命令参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆