【问题标题】:Xamarin MVVM, binding ListView item as command parameterXamarin MVVM,将 ListView 项绑定为命令参数
【发布时间】:2021-02-02 07:18:34
【问题描述】:

我有一个从视图模型填充的 ListView,我现在可以轻松地将数据模板的元素绑定到数据源项的属性,并将这些属性发送到视图模型中的命令。但是我想应该能够发送整个源项目。但是,当我发送除字符串以外的任何内容时,应用程序崩溃或按钮都被禁用并且似乎没有触发命令。这个 OOP 和 xamarin newb 将非常感谢任何指针/帮助。我怀疑这只是按钮中的语法,或者我不明白的命令中的语法,我目前拥有的是:

<ContentPage.BindingContext>
    <local:TestViewModel/>
</ContentPage.BindingContext>
<ContentPage.Content>
    <StackLayout  >
        <Label Text="{Binding SelectedItem.Term_id}"
            VerticalOptions="CenterAndExpand" 
            HorizontalOptions="CenterAndExpand" />
        <BoxView HeightRequest="1" BackgroundColor="Black" HorizontalOptions="FillAndExpand" />
        <ListView x:Name="propListView"
            SelectedItem="{Binding SelectedItem}"
            ItemsSource="{Binding SimpleProperties}"  >
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell x:Name="theItem" >
                        <StackLayout Orientation="Horizontal">
                            <Button Text="{Binding Agt_name}"
                              Command="{Binding BindingContext.doCheckin, Source={x:Reference Page}}"
                              CommandParameter="{Binding BindingContext.doCheckin, Source={x:Reference theItem}, Mode=OneWayToSource}"
                                    />
                            <Label Text="{Binding MilesDistance, StringFormat='~{0:f2} mi.'},
                        Converter={ConvertToDouble}}" HorizontalOptions="CenterAndExpand" />
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>
</ContentPage.Content>

...

using AgentApp.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows.Input;
using Xamarin.Forms;

namespace AgentApp
{
    class TestViewModel : BaseViewModel
    {
        private ObservableCollection<Property> simpleProperties;
        public ObservableCollection<Property> SimpleProperties { set { SetProperty(ref simpleProperties, value); } get { return simpleProperties; } }

        private Property selectedItem;
        public Property SelectedItem { get { return selectedItem; } set { SetProperty(ref selectedItem, value); } }

        public ICommand setSelection { private set; get; }
        public ICommand doCheckin { private set; get; }
        public TestViewModel()
        {
            var SimplePropertiesList = new List<Property>();
            for (int i = 1; i <= 5; i++)
            {
                Property prop = new Property();
                prop.Term_id = i.ToString();
                prop.Agt_name = "Prop " + i.ToString();
                prop.MilesDistance = (double)i * 2.5;
                SimplePropertiesList.Add(prop);
            }
            SimpleProperties = new ObservableCollection<Property>(SimplePropertiesList);

            /* for buttons:*/
            doCheckin = new Command<Property>(
                execute: (Property item) =>
                {
                    Console.WriteLine("doCheckin() executes: " + item.Term_id);
                    SelectedItem = item;
                },
            canExecute: (Property item) =>
            {
                Console.WriteLine("doCheckin() canExecute return" + item.Term_id);
                return (SelectedItem != item); 
            }
                );
        }
    }
}

当然

class BaseViewModel : INotifyPropertyChanged
    {
        public bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
        {
            if (Object.Equals(storage, value)) return false;
            storage = value;
            OnPropertyChanged(propertyName);
            return true;
        }
        public virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
        public event PropertyChangedEventHandler PropertyChanged;
    }

提前致谢!

【问题讨论】:

  • "{Binding .}" 引用当前项

标签: c# xamarin mvvm data-binding


【解决方案1】:

但是我想应该能够发送整个源项目。但是,当我发送除字符串以外的任何内容时,应用程序崩溃或按钮都被禁用并且似乎没有触发命令。

根据您的描述,您想在单击按钮时传递当前项目,对吗?如果是,正如 Jason 所说,像这样修改 Button CommandParameter:

  <ListView
            x:Name="propListView"
            ItemsSource="{Binding SimpleProperties}"
            SelectedItem="{Binding selectedItem}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout Orientation="Horizontal">
                            <Button
                                Command="{Binding BindingContext.doCheckin, Source={x:Reference page27}}"
                                CommandParameter="{Binding .}"
                                Text="{Binding Agt_name}" />
                            <Label HorizontalOptions="CenterAndExpand" Text="{Binding MilesDistance}" />
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

 doCheckin = new Command((e) =>
        {
            var item = (e as Property);
            Console.WriteLine("the property Term_id is {0} ", item.Term_id);
        });

顺便说一下,选中的item需要实现INotifyPropertychanged来更新数据。

  private Property _selectedItem;
    public Property selectedItem 
    { 
        get { 
            return _selectedItem; 
        } 
        set { 
            _selectedItem=value;
            OnPropertyChanged("selectedItem");
        } 
    }      

【讨论】:

  • 谢谢@Cherry-bu-msft,我来对了。我在此 ViewModel 扩展的 BaseViewModel 类中实现 OnPropertyChanged。我还缺少的是调用 (doCheckin as Command).ChangeCanExecute(); 来更改基础变量。
猜你喜欢
  • 2018-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-17
  • 2023-01-12
  • 2014-09-01
  • 1970-01-01
  • 2010-12-23
相关资源
最近更新 更多