MVVM - ItemsControl中Button的CommandParameter [英] MVVM - CommandParameter for Button within ItemsControl

查看:87
本文介绍了MVVM - ItemsControl中Button的CommandParameter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在使用MVVM设计模式开发WPF应用程序。

在应用程序中我有一个ItemsControl,它显示了几个按钮(按钮的数量不是常量,根据应用程序的逻辑流程。

ItemsControl绑定到ViewModel中的LinkedList。

我能够为按钮定义一个命令,我能够仅当Command没有参数时才能在ViewModel中获取Command处理程序...

我需要知道哪个按钮被点击了,因此我想要一个带参数的命令。





我如何定义我的视图?

CommandParameter的语法是什么,我用来获取Button内容的参数是什么?

ViewModel中的命令签名是什么?



请注意我使用的是MVVM Light的RelayCommand。



View的relvant代码是这里。

提前致谢



Hi,
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.


How do I need to deine 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 ?

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


这篇关于MVVM - ItemsControl中Button的CommandParameter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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