【发布时间】: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