如何在 Silverlight / XAML 中让多个控件触发相同的事件/命令?
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定我是否理解正确,但您可以在 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
我刚刚使用以下 XAML 运行测试,它似乎成功运行:-
您的按钮 XAML:-
您的资源 XAML:-
希望这会有所帮助
I have just run a test using the following XAML and it seems to work successfully :-
Your Button XAML :-
Your Resource XAML :-
Hope this helps