事件命令不起作用

发布于 2024-10-15 19:29:43 字数 1577 浏览 2 评论 0原文

我正在使用 Galasoft MVVM Light 工具包在 MVVM 中制作一个应用程序。但是我无法使 EventToCommand 与 Telerik 上下文菜单一起使用。这是我的代码:-

   <ListBox x:Name="lstPhotoAlbums" ItemsSource="{Binding AlbumsCollection}" 
                         Margin="3,0" Grid.Row="1" ItemsPanel="{StaticResource wrapPanelItemsPanelTemplate}"
                         ItemTemplate="{StaticResource ListboxPhotosDataTemplate}" 
                         ItemContainerStyle="{StaticResource ListboxPhotoAlbumsContainerStyle}" Height="500" HorizontalAlignment="Left" VerticalAlignment="Top" Width="178">
                        <telerik:RadContextMenu.ContextMenu>
                            <telerik:RadContextMenu x:Name="albumsCtxMenu">
                                <telerik:RadMenuItem Header="Delete" >
                                    <i:Interaction.Triggers>
                                        <i:EventTrigger EventName="Click">
                                            <GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding DeleteAlbumCommand}"  CommandParameter="{Binding SelectedItem, ElementName=lstPhotoAlbums}"/>
                                        </i:EventTrigger>
                                    </i:Interaction.Triggers>
                                </telerik:RadMenuItem>
                            </telerik:RadContextMenu>
                        </telerik:RadContextMenu.ContextMenu>
                    </ListBox>

我确实在视图模型中遇到了断点。但是命令参数始终为空。有什么想法我错了吗?

提前致谢 :)

I am making one application in MVVM using Galasoft MVVM Light toolkit. However i can't make EventToCommand make it work with Telerik Context Menu. Here is my code :-

   <ListBox x:Name="lstPhotoAlbums" ItemsSource="{Binding AlbumsCollection}" 
                         Margin="3,0" Grid.Row="1" ItemsPanel="{StaticResource wrapPanelItemsPanelTemplate}"
                         ItemTemplate="{StaticResource ListboxPhotosDataTemplate}" 
                         ItemContainerStyle="{StaticResource ListboxPhotoAlbumsContainerStyle}" Height="500" HorizontalAlignment="Left" VerticalAlignment="Top" Width="178">
                        <telerik:RadContextMenu.ContextMenu>
                            <telerik:RadContextMenu x:Name="albumsCtxMenu">
                                <telerik:RadMenuItem Header="Delete" >
                                    <i:Interaction.Triggers>
                                        <i:EventTrigger EventName="Click">
                                            <GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding DeleteAlbumCommand}"  CommandParameter="{Binding SelectedItem, ElementName=lstPhotoAlbums}"/>
                                        </i:EventTrigger>
                                    </i:Interaction.Triggers>
                                </telerik:RadMenuItem>
                            </telerik:RadContextMenu>
                        </telerik:RadContextMenu.ContextMenu>
                    </ListBox>

I do hit the breakpoint in my viewmodel. However the command parameter is always null. Any ideas where i am wrong?

Thanks in advance :)

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

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

发布评论

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

评论(1

握住你手 2024-10-22 19:29:43

由于这是一篇旧帖子,您可能已经找到了问题的答案。然而,当我尝试做同样的事情时,我没有找到准确的答案,如果其他人也在寻找同样的答案,我希望这可以帮助他们。

您需要从 EventToCommand 中删除 CommandParameter 参数,并将其更改为:

<GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding DeleteAlbumCommand}" PassEventArgsToCommand="True" />

ViewModel 中的 RelayCommand 或实现 RelayCommand 的任何位置都必须如下所示:

RelayCommand<EventArgs> DeleteAlbumCommand = new RelayCommand<EventArgs>(CallbackMethod);

CallbackMethod 应该如下所示:

private void CallbackMethod(EventArgs eventArgs)
{
...
}

希望这会有所帮助。

As this is an old post, you might have found the answer to your question. However, as I was trying to do the same, I did not find a precis answer to this and if others are looking for the same, I hope this might help them.

You will need to remove the CommandParameter argument from your EventToCommand and change it to this:

<GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding DeleteAlbumCommand}" PassEventArgsToCommand="True" />

Your RelayCommand in your ViewModel or whereever you are implementing your RelayCommand would have to look something like this:

RelayCommand<EventArgs> DeleteAlbumCommand = new RelayCommand<EventArgs>(CallbackMethod);

CallbackMethod should then look something like this:

private void CallbackMethod(EventArgs eventArgs)
{
...
}

Hope this will help.

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