2

我正在使用 MVVMLight 开发 Win10 UWP 应用程序(我从未使用过 MVVMLight,也从未“正确”完成命令)。我有一个绑定到 ObservableCollection 的 ItemsControl。参与者有两个属性 - 名称和圈数。我在 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>

视图模型创建了一个名为 Participants 的 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 的 Participants 属性以显示,因为我的视图没有更新。这是 Participants 属性:

    /// <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 迄今为止的帮助!

4

1 回答 1

1

尝试这个:

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

只要您写的所有内容都绝对正确,就没有太多要向您解释的东西。

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

于 2016-04-19T21:39:18.823 回答