GalaSoft_MvvmLight_Command:ListItem 上按钮上的 EventToCommand?

发布于 2024-11-27 22:11:48 字数 1824 浏览 1 评论 0原文

将 mvvm-light EventToCommand 连接到数据模板中的行项目的语法是什么?对于主模型上的操作,如下所示的语法工作正常,但是如果我对数据模板中的行项目执行操作,则绑定不起作用,并且我需要识别要操作的特定行项目。

在尝试将事件连接到命令之前,我将行项目单击连接到 XAML 代码隐藏中的事件处理程序;处理程序从事件参数中提取行项目数据对象,然后通过 DataContext 将行项目数据对象传递给视图模型的方法,效果很好,但我希望与整个应用程序的处理保持一致。

输出中的运行时错误: System.Windows.Data 错误:BindingExpression 路径错误:在“Model.LineItem”上找不到“EditLineCommand”属性。 BindingExpression: Path='EditLineCommand' DataItem='Model.LineItem';目标元素是“System.Windows.Controls.Button”(名称=“EditRowButton”);目标属性是“DependencyPropertyListener39”(类型“System.Object”)..

XAML main layout:
<!-- Line Items -->
<ListBox ItemTemplate="{StaticResource LineItemTemplate}" ItemsSource="{Binding Model.LineItems}"/>

XAML data template:
<DataTemplate x:Key="LineItemTemplate">
<Button>
<Image Source="..." />
<Custom:Interaction.Triggers>
<Custom:EventTrigger EventName="Click">
<GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding EditLineCommand, Mode=OneWay}" />
</Custom:EventTrigger>
</Custom:Interaction.Triggers>
</Button>
</DataTemplate>

更新 - 我想我已经快到了,@Claus 的回答通过解决命令绑定问题让我获得了大部分帮助。为了识别要操作的特定行,我绑定到 LineItem 的 LineNumber,然后在中继命令上拉出该参数:

<GalaSoft_MvvmLight_Command:EventToCommand 
Command="{Binding DataContext.DeleteLineCommand, ElementName=DetailPage}"                                                                       
CommandParameter="{Binding LineNumber}"
PassEventArgsToCommand="True" />

...

public RelayCommand<int> DeleteLineCommand { get; private set; }

...

DeleteLineCommand = new RelayCommand<int>((ln) => { DeleteLineItem(ln); });

这是一个可行的解决方案,但是有没有办法绑定到完整的行LineItem 而不仅仅是一个成员?

What is the syntax for hooking up a mvvm-light EventToCommand on a line item in a data template? For an action on the main model, syntax like the below works fine, however if I am doing an operation on a line item in a data template, the binding isn't working, and I need to identify the specific line item to operate on.

Before attempting to hook up the Event to Command I had the line item click hooked to an event handler in the XAML codebehind; the handler extracted the line item data object out of the event args, and then passed the line item data object to a method via the DataContext to the view model and that worked fine, but I wanted to stay consistent with handling across the app.

Runtime error in output:
System.Windows.Data Error: BindingExpression path error: 'EditLineCommand' property not found on 'Model.LineItem'. BindingExpression: Path='EditLineCommand' DataItem='Model.LineItem'; target element is 'System.Windows.Controls.Button' (Name='EditRowButton'); target property is 'DependencyPropertyListener39' (type 'System.Object')..

XAML main layout:
<!-- Line Items -->
<ListBox ItemTemplate="{StaticResource LineItemTemplate}" ItemsSource="{Binding Model.LineItems}"/>

XAML data template:
<DataTemplate x:Key="LineItemTemplate">
<Button>
<Image Source="..." />
<Custom:Interaction.Triggers>
<Custom:EventTrigger EventName="Click">
<GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding EditLineCommand, Mode=OneWay}" />
</Custom:EventTrigger>
</Custom:Interaction.Triggers>
</Button>
</DataTemplate>

Update - I think I'm almost there, @Claus' answer got me most of the way by solving the Command binding problem. To identify the specific line to operate on, I bind to the LineNumber of the LineItem and then pull that parameter out on the relay command:

<GalaSoft_MvvmLight_Command:EventToCommand 
Command="{Binding DataContext.DeleteLineCommand, ElementName=DetailPage}"                                                                       
CommandParameter="{Binding LineNumber}"
PassEventArgsToCommand="True" />

...

public RelayCommand<int> DeleteLineCommand { get; private set; }

...

DeleteLineCommand = new RelayCommand<int>((ln) => { DeleteLineItem(ln); });

This is a workable solution, but is there a way to bind to the full LineItem rather than just a member?

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

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

发布评论

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

评论(2

笔落惊风雨 2024-12-04 22:11:48

请参阅将数据网格绑定到一个ViewModel、列/组合框另一个讨论如何从数据模板绑定到视图模型的属性(或命令)。

See Bind datagrid to one ViewModel, column / combobox to another for a discussion on how to bind to a property (or command) of a view model from a data template.

匿名。 2024-12-04 22:11:48

魔术:

<phone:PhoneApplicationPage x:Name="MyPage" ... >
    ...
    <GalaSoft_MvvmLight_Command:EventToCommand 
        Command="{Binding DataContext.EditLineCommand, ElementName=MyPage}" />
    ...
</phone:PhoneApplicationPage>

这样,它将使用页面的 DataContext,通常是您的 ViewModel。

Magic trick:

<phone:PhoneApplicationPage x:Name="MyPage" ... >
    ...
    <GalaSoft_MvvmLight_Command:EventToCommand 
        Command="{Binding DataContext.EditLineCommand, ElementName=MyPage}" />
    ...
</phone:PhoneApplicationPage>

That way, it'll use the Page's DataContext, which usually is your ViewModel.

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