繁体   English   中英

将命令绑定到 ItemsControl 项

[英]Binding Command to ItemsControl Item

我正在使用 MVVMLight 开发 Win10 UWP 应用程序(我从未使用过 MVVMLight,也从未“正确”执行过命令)。 我有一个绑定到 ObservableCollection 的 ItemsControl。 Participant 有两个属性 - Name 和 Las。 我在 ItemsControl.ItemTemplate 中有控件来显示按钮(减去)、项目的 Name 属性、项目的 Laps 属性和另一个按钮(添加)。 这是 XAML:

<ItemsControl
                    ItemsSource="{Binding Participants, Mode= TwoWay}">
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <Grid
                                MinWidth="300"
                                Margin="0, 12">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition
                                        Width="2*"></ColumnDefinition>
                                    <ColumnDefinition
                                        Width="6*"></ColumnDefinition>
                                    <ColumnDefinition
                                        Width="2*"></ColumnDefinition>
                                    <ColumnDefinition
                                        Width="3*"></ColumnDefinition>
                                </Grid.ColumnDefinitions>
                                <Button
                                    Content="&#xE108;"
                                    FontFamily="Segoe MDL2 Assets"
                                    FontSize="20"
                                    Grid.Column="0"
                                    Margin="0, 0, 12, 0"></Button>
                                <TextBlock
                                    Text="{Binding Path=Name, Mode=TwoWay}"
                                    FontSize="20"
                                    FontWeight="Bold"
                                    Grid.Column="1"></TextBlock>
                                <TextBlock
                                    Text="{Binding Laps, Mode=TwoWay}"
                                    FontSize="20"
                                    FontWeight="Bold"
                                    Grid.Column="2"></TextBlock>
                                <Button
                                    Content="&#xE710;"
                                    FontFamily="Segoe MDL2 Assets"
                                    FontSize="20"
                                    Command="{Binding AddLapCommand}"
                                    CommandParameter="{Binding}"
                                    Grid.Column="3"
                                    Margin="12, 0, 0, 0"></Button>
                            </Grid>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>

视图模型创建一个名为参与者的 ObservableCollection。 我正在尝试将添加按钮(当然还有减去按钮)绑定到 VM 中的 RelayCommand。 我已经尝试了很多东西,所以我不能在这里发布我尝试过的所有东西。 这是我所拥有的最新内容,(但它仍然不起作用):

    public RelayCommand<object> AddLapCommand
    {
        get
        {
            if (_addLapCommand == null)
            {
                _addLapCommand = new RelayCommand<object>((e) => ExecuteAddLapCommand(e));
            }
            return _addLapCommand;
        }
    }

    private void ExecuteAddLapCommand(object o)
    {
        throw new NotImplementedException();
    }

xaml 中的智能感知告诉我它无法解析 LapCounter.Model.Participant 类型的数据上下文中的属性 AddLapCommand。 我不是要访问 Participant 类,而是要访问 HomeViewModel 类,它是页面的 DataContext。 我想我可以从 ItemsControl 绑定到的集合中的单个项目中看到它从哪里获取 LapCounter.Model.Participant。 但我的印象是,如果找不到 DataContext,它会继续沿着可视化树向上,直到找到正确的。 这是页面声明中的 DataContext:

DataContext="{Binding Home, Source={StaticResource Locator}}"

如何让它查看 RelayCommand 的 VM? 我需要做的是让按钮将它表示的参与者作为参数发送给 RelayCommand,并使用它来增加(在减法按钮的情况下减少)该特定参与者的圈数整数。

我很感激我能得到的任何帮助。 谢谢!

编辑从 VM 添加了参与者属性以显示,因为我的视图没有更新。 这是参与者属性:

    /// <summary>
    /// The <see cref="Participants" /> property's name.
    /// </summary>
    public const string ParticipantsPropertyName = "Participants";

    private ObservableCollection<Participant> _participants = new ObservableCollection<Participant>();

    /// <summary>
    /// Sets and gets the Participants property.
    /// Changes to that property's value raise the PropertyChanged event. 
    /// </summary>
    public ObservableCollection<Participant> Participants
    {
        get
        {
            return _participants;
        }

        set
        {
            if (_participants == value)
            {
                return;
            }

            _participants = value;
            RaisePropertyChanged(() => Participants);
        }
    }

感谢 Eldar Dordzhiev 到目前为止的帮助!

尝试这个:

Command="{Binding Home.AddLapCommand, Source={StaticResource Locator}}"

只要你写的一切都是绝对正确的,就没有什么要向你解释的了。

至于递增\\递减Laps ,您可以简单地将Participant传递给命令处理程序并更改Laps 如果您使用通过RelayCommand<Participant>完成的RelayCommand<Participant> 不要忘记在CommandParameter传递Participant

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM