如何将 RelayCommand 分配给 Click 或 SelectedIndexChanges 事件?

发布于 2024-08-30 14:46:11 字数 703 浏览 1 评论 0原文

我刚刚开始使用 MVVM light,但到目前为止它让我解决了一些问题。不幸的是,我在 Silverlight 中遇到了相对简单的问题。

让我们假设以下带有 EventToCommand 的按钮:

<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseEnter">
<GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding UpdateAccountsCommand, Mode=OneWay}" CommandParameter="{Binding SelectedIndex, ElementName=lstLedgers}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>

如何将此代码分配给 SelectedIndexChanged 事件?

进一步的问题 - 如何在 C# 代码中分配命令? 问题如下:我正在开发 Windows Phone 7 应用程序。应用程序栏需要在C#中启动(据我所知,现阶段没有应用程序栏的xaml代码)。因此,我不知道如何将命令绑定到 c#(现在是 xaml)中的应用程序 bat 按钮。

预先感谢您的帮助。

I'm just starting with MVVM light, but so far it allowed me to solve some of my issues. Infortunately I'm struggling with relatively sime issues in Silverlight.

Let's assume the following button with EventToCommand:

<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseEnter">
<GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding UpdateAccountsCommand, Mode=OneWay}" CommandParameter="{Binding SelectedIndex, ElementName=lstLedgers}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>

How to assign this code to SelectedIndexChanged event?

Futher issue - how to assign a command in C# code? The problem is as follows: I'm developing Windows Phone 7 app. Application Bar needs to be initiated in C# (as far as I know there is no xaml code for application bar at this stage). As a result I have no idea how to bind a command to a application bat button in from c#, now xaml.

Thanks in advance your your help.

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

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

发布评论

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

评论(1

可是我不能没有你 2024-09-06 14:46:11

如何将此代码分配给 SelectedIndexChanged 事件

事件名称位于触发器的初始 xaml 行中。 EventName 是触发事件发生的原因。您需要确保在具有 SelectedIndexChanged 事件的对象上有此触发器。按钮没有该事件。

...
<i:EventTrigger EventName="SelectedIndexChanged"> 
...

如果您要破坏应用程序栏的 mvvm,我不知道您为什么要创建触发器。您不妨走直接路线,直接访问您想要的事件。你可以做到,不要误会我的意思......只是这段代码更干净:

ApplicationBarButton.Click += new RoutedEventHandler(OnApplicationBarButton_Click);

How to assign this code to SelectedIndexChanged event

The event name is in the trigger's initial xaml line. The EventName is what triggers something to happen. You'll need to make sure that you have this trigger on an object with a SelectedIndexChanged event. Button doesn't have that event.

...
<i:EventTrigger EventName="SelectedIndexChanged"> 
...

If you are breaking mvvm for the application bar, I don't know why you'd want to create a trigger. You might as well go the direct route by accessing the events you want directly. You can do it, don't get me wrong... just this code is a lot cleaner:

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