您如何知道 ItemsControl 的哪个元素在 MVVM 中发送事件?

发布于 2024-11-28 15:36:54 字数 740 浏览 1 评论 0原文

假设我当前有一个 ItemsControl,其 DataTemplate 是一堆按钮。我正在连接这些按钮的单击事件,但是我如何知道单击了哪个按钮?我不应该使用 ItemsControl 吗?

我试图不使用任何代码隐藏,但务实可能是必要的。

<ItemsControl>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Button Margin="10">
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="Click">
                        <GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding ItemsControlButtonClicked, Mode=OneWay}"/>
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </Button>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

Let's say I currently have an ItemsControl whose DataTemplate is a bunch of buttons. I'm wiring up these buttons' click events, but how am I to know which button was clicked? Should I not use a ItemsControl?

I'm trying to have no code-behind, but being pragmatic may be necessary.

<ItemsControl>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Button Margin="10">
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="Click">
                        <GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding ItemsControlButtonClicked, Mode=OneWay}"/>
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </Button>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

花期渐远 2024-12-05 15:36:54

如果您想知道单击了什么 Item,请将 {Binding } 作为 CommandParameter 传递,它会将所选对象传递给您的 Command

If如果您想知道单击了什么 Button,我会在代码隐藏中执行此操作,因为 ViewModel 不需要了解有关 UI 的任何信息,其中包括按钮。

此外,由于您的控件是按钮,因此您应该使用 Command 属性而不是 Click 触发器。

<Button Command="{Binding ItemsControlButtonClicked}" />

If you want to know what Item was clicked, then pass {Binding } as the CommandParameter and it will pass the selected object to your Command

If you want to know what Button was clicked, I would do that in the code-behind since ViewModels do not need to know anything about the UI, and that includes buttons.

Also since your control is a Button, you should use the Command property instead of a Click trigger.

<Button Command="{Binding ItemsControlButtonClicked}" />
给我一枪 2024-12-05 15:36:54

您可以随命令一起发送参数,并根据这些参数您可以找出单击了哪个按钮

You can send parameters along with the command and based on these parameters you can find out which button was clicked

酸甜透明夹心 2024-12-05 15:36:54

在我的项目中,我还使用 MVVM Light 我有一个包含项目集合的下拉菜单,以及一个用户按下的按钮和操作取决于从下拉菜单中选择的项目
您应该创建一个带有参数的 Relay 命令,请查看我的代码中的示例,

  public RelayCommand<Project> StartTimer { get; private set; }//declare command

   StartTimer = new RelayCommand<Project>(OnStartTimer);

    private void OnStartTimer(Project project)
    {

        if (project != null)
        {

            currentProject = project;

            if (!timer.IsTimerStopped)
            {
                timer.StopTimer();
            }
            else
            {
                Caption = "Stop";
                timer.StartTimer();
            }
        }

我将下拉列表与类 Project 的集合绑定在一起
对于按钮命令参数,我绑定所选项目表单下拉列表
查看代码,

   <ComboBox Name="projectcomboBox" ItemsSource="{Binding Path=Projects}"    IsSynchronizedWithCurrentItem="True" DisplayMemberPath="FullName"
              SelectedValuePath="Name"  SelectedIndex="0"  >
    </ComboBox>
      <Button Name="timerButton" Content="{Binding Path=Caption}" Command="{Binding Path=StartTimer}" 
                CommandParameter="{Binding ElementName=projectcomboBox, Path=SelectedItem}"  ></Button>

注意 Command 和 CommandParameter 绑定

,您也可以使用此方法,不仅用于下拉菜单

In my project I also use the MVVM Light I has an dropdown with collection of items, and a button which user press and action depend on selected item from drop down
you should create a Relay command with parameter look at the example from my code

  public RelayCommand<Project> StartTimer { get; private set; }//declare command

   StartTimer = new RelayCommand<Project>(OnStartTimer);

    private void OnStartTimer(Project project)
    {

        if (project != null)
        {

            currentProject = project;

            if (!timer.IsTimerStopped)
            {
                timer.StopTimer();
            }
            else
            {
                Caption = "Stop";
                timer.StartTimer();
            }
        }

on the view I bind the drop down with collection of class Project
and for button command parameter I bind the selected item form drop down
look at the code

   <ComboBox Name="projectcomboBox" ItemsSource="{Binding Path=Projects}"    IsSynchronizedWithCurrentItem="True" DisplayMemberPath="FullName"
              SelectedValuePath="Name"  SelectedIndex="0"  >
    </ComboBox>
      <Button Name="timerButton" Content="{Binding Path=Caption}" Command="{Binding Path=StartTimer}" 
                CommandParameter="{Binding ElementName=projectcomboBox, Path=SelectedItem}"  ></Button>

pay attention to Command and CommandParameter binding

also you can use this approache not only for drop down

遮云壑 2024-12-05 15:36:54

那么,您可以使用 Sender.DataContext 这是实际的数据。

Well, you can use the Sender.DataContext which is the actual data.

思念满溢 2024-12-05 15:36:54

在视图模型类中创建命令属性(使用 Josh Smith 的 RelayCommand 模式是执行此操作的最简单方法)并将每个按钮的 Command 绑定到相应的按钮。这不仅简单易行且易于维护,而且还为您提供了一种在需要时实现启用/禁用行为的简单方法。

Create command properties in your view model class (using Josh Smith's RelayCommand pattern is the simplest way to do this) and bind each button's Command to the appropriate one. Not only is this straightforward to do and simple to maintain, it also gives you an easy way of implementing the enable/disable behavior when you need to.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文