如何在 Silverlight / XAML 中让多个控件触发相同的事件/命令?

发布于 2024-10-12 21:41:24 字数 1988 浏览 3 评论 0原文

我正在使用 Mvvm-Light 命令将事件从我的视图路由到我的 ViewModel。效果很好。

但我想我可以更优雅。这实际上可能只是一个标准的 XAML 问题:

我想更改此设置(10 个按钮均带有 Click="keypadButton_Click" 作为其事件处理程序):

<Button  Name="button1" Content="1" Grid.Row="0" Grid.Column="0" Click="keypadButton_Click" />
<Button  Name="button2" Content="2" Grid.Row="0" Grid.Column="1" Click="keypadButton_Click"/>
<Button  Name="button3" Content="3" Grid.Row="0" Grid.Column="2" Click="keypadButton_Click"/>
<Button  Name="button4" Content="4" Grid.Row="1" Grid.Column="0" Click="keypadButton_Click"/>
<Button  Name="button5" Content="5" Grid.Row="1" Grid.Column="1" Click="keypadButton_Click"/>
<Button  Name="button6" Content="6" Grid.Row="1" Grid.Column="2" Click="keypadButton_Click"/>
<Button  Name="button7" Content="7" Grid.Row="2" Grid.Column="0" Click="keypadButton_Click"/>
<Button  Name="button8" Content="8" Grid.Row="2" Grid.Column="1" Click="keypadButton_Click"/>
<Button  Name="button9" Content="9" Grid.Row="2" Grid.Column="2" Click="keypadButton_Click"/>
<Button  Name="button0" Content="0" Grid.Row="3" Grid.Column="1" Click="keypadButton_Click"/>

更改为其中 10 个:

<Button  x:Name="button1" Content="1" Grid.Row="0" Grid.Column="0">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Click">
            <Command:EventToCommand 
               Command="{Binding KeyPadButtonCommand}"
               CommandParameter="{Binding ElementName=button1, Path=Content }"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
</Button>

但我不希望这样很多 XAML。我认为有一种方法可以“宏观化”触发器,因此它看起来像这样:

<Button  Name="button1" Content="1" Grid.Row="0" Grid.Column="0" 
         Click="{Binding _SendTheEventHere_}" />

其中 "{Binding _SendTheEventHere_}" 应该替换为一些我一无所知的魔法咒语,但是我知道 stackoverflow 上的一些向导知道:-)。

有道理吗?

I am using Mvvm-Light Commands to route events from my Views to my ViewModels. Works great.

But I think I can be more elegant. This is actually probably just a standard XAML question:

I want to change this (10 buttons all with Click="keypadButton_Click" for their event handler):

<Button  Name="button1" Content="1" Grid.Row="0" Grid.Column="0" Click="keypadButton_Click" />
<Button  Name="button2" Content="2" Grid.Row="0" Grid.Column="1" Click="keypadButton_Click"/>
<Button  Name="button3" Content="3" Grid.Row="0" Grid.Column="2" Click="keypadButton_Click"/>
<Button  Name="button4" Content="4" Grid.Row="1" Grid.Column="0" Click="keypadButton_Click"/>
<Button  Name="button5" Content="5" Grid.Row="1" Grid.Column="1" Click="keypadButton_Click"/>
<Button  Name="button6" Content="6" Grid.Row="1" Grid.Column="2" Click="keypadButton_Click"/>
<Button  Name="button7" Content="7" Grid.Row="2" Grid.Column="0" Click="keypadButton_Click"/>
<Button  Name="button8" Content="8" Grid.Row="2" Grid.Column="1" Click="keypadButton_Click"/>
<Button  Name="button9" Content="9" Grid.Row="2" Grid.Column="2" Click="keypadButton_Click"/>
<Button  Name="button0" Content="0" Grid.Row="3" Grid.Column="1" Click="keypadButton_Click"/>

To 10 of these:

<Button  x:Name="button1" Content="1" Grid.Row="0" Grid.Column="0">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Click">
            <Command:EventToCommand 
               Command="{Binding KeyPadButtonCommand}"
               CommandParameter="{Binding ElementName=button1, Path=Content }"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
</Button>

But I don't want that much XAML. I would think there would be a way to "macroize" the Trigger so it would look something like this:

<Button  Name="button1" Content="1" Grid.Row="0" Grid.Column="0" 
         Click="{Binding _SendTheEventHere_}" />

Where "{Binding _SendTheEventHere_}" should be replaced with some magic incantation that I am clueless about, but I know some wizard here on stackoverflow knows :-).

Make sense?

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

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

发布评论

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

评论(2

两人的回忆 2024-10-19 21:41:24

我不确定我是否理解正确,但您可以在 MVVM Light Toolkit 中使用 RelayCommand 这里是示例代码

一般来说,RelayCommand 是您可以使用的 ICommand 接口的实现

I'm not sure if I understand you correctly, but you can use RelayCommand in MVVM Light Toolkit here is a sample code

Generally RelayCommand are implementation of ICommand interface that you can use

一江春梦 2024-10-19 21:41:24

我刚刚使用以下 XAML 运行测试,它似乎成功运行:-

您的按钮 XAML:-

<Button Name="MyConfirmButton" 
        Content="Confirm" 
        Grid.Column="1" 
        Style="{StaticResource ButtonStyle}"
        i:Interaction.Triggers="{StaticResource PerformConfirm}">
</Button>

您的资源 XAML:-

<UserControl.Resources>
   <i:EventTrigger x:Key="PerformConfirm" EventName="Click">
      <cmd:EventToCommand Command="{Binding Path=PerformConfirm}" />
   </i:EventTrigger>
</UserControl.Resources>

希望这会有所帮助

I have just run a test using the following XAML and it seems to work successfully :-

Your Button XAML :-

<Button Name="MyConfirmButton" 
        Content="Confirm" 
        Grid.Column="1" 
        Style="{StaticResource ButtonStyle}"
        i:Interaction.Triggers="{StaticResource PerformConfirm}">
</Button>

Your Resource XAML :-

<UserControl.Resources>
   <i:EventTrigger x:Key="PerformConfirm" EventName="Click">
      <cmd:EventToCommand Command="{Binding Path=PerformConfirm}" />
   </i:EventTrigger>
</UserControl.Resources>

Hope this helps

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