【问题标题】:Binding Command to ItemsControl Item将命令绑定到 ItemsControl 项
【发布时间】:2016-08-12 07:24:28
【问题描述】:

我正在使用 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 迄今为止的帮助!

【问题讨论】:

    标签: c# xaml win-universal-app mvvm-light uwp-xaml


    【解决方案1】:

    试试这个:

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

    只要你写的每件事都绝对正确,没有什么需要向你解释的。

    至于递增/递减Laps,您可以简单地将Participant 传递到命令处理程序并更改Laps。如果您使用通过RelayCommand&lt;Participant&gt; 完成的MVVMLight。不要忘记在CommandParameter 中传递Participant。

    【讨论】:

    • 谢谢,埃尔达。那解决了那个问题。现在我的 RelayCommand 工作了,Participant 的 Laps 属性也更新了。现在我遇到的问题是视图上的 Laps 显示没有更新。我已经从 Participants 属性的 VM 添加了代码,它实现了 RaisePropertyChanged,但它不起作用。如果集合中的项目发生更改,集合不会 RaisePropertyChanged 吗?
    • 没关系 - 感谢这篇文章 (stackoverflow.com/questions/5285377/…),我想通了。现在可以了。非常感谢!
    • @RichHopkins 你需要在Participant 类中实现INotifyPropertyChanged。如果对您有帮助,请不要忘记将我的答案标记为答案。
    猜你喜欢
    • 1970-01-01
    • 2020-12-21
    • 1970-01-01
    • 2014-10-21
    • 2021-11-05
    • 2010-11-06
    • 2018-11-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多