【发布时间】:2021-06-01 09:41:22
【问题描述】:
我想从具有 ViewModel 的 ViewCell 中绑定我的 MenuItem。
这是我的视图单元格,有人知道这种绑定的正确方法吗?
我有一个ObservableCollection<object> 填充我的ListView,这是我需要绑定的单元格。
<?xml version="1.0" encoding="UTF-8" ?>
<ViewCell
x:Class="DataTemp.Controls.DeviceViewCell"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Name="this">
<ViewCell.ContextActions>
// I've been trying these two ways
<MenuItem Command="{Binding Source={x:Reference this}, Path=BindingContext.DeviceCommand}" Text="Call" />
<MenuItem Command="{Binding DeviceCommand}" Text="Call" />
</ViewCell.ContextActions>
<ViewCell.View>
<StackLayout Padding="10">
<Label Text="{Binding Name}" />
<Label Text="{Binding Number}" />
</StackLayout>
</ViewCell.View>
</ViewCell>
这是我的视图模型 我试图用断点捕捉事件,但我从未进入 OnDeviceCommnad 方法。
public class DeviceViewCellViewModel : BaseViewModel, IDevice
{
public string Number
{
get
{
return _numberDevice;
}
set
{
_numberDevice = value;
OnPropertyChanged("Number");
}
}
public string Name
{
get
{
return _nameDevice;
}
set
{
_nameDevice = value;
OnPropertyChanged("Number");
}
}
public ICommand DeviceCommand { get; set; }
private string _nameDevice;
private string _numberDevice;
public DeviceViewCellViewModel()
{
DeviceCommand = new Command(OnDeviceCommnad);
}
private void OnDeviceCommnad()
{
}
}
【问题讨论】:
-
输出控制台中是否显示任何绑定错误?例如:
[0:] Binding: 'LoginClickeddCommand' property not found on 'xxxx.ViewModels.LoginViewModel', target property: 'xxxx.Controls.CustomButton.Command' -
不,一点也不,但也不让我执行命令@Andrew
-
嗨,ViewCell 里面的标签是否显示?您可以在这里分享示例项目链接,我会检查一下。
-
@JuniorJiang-MSFT 是的先生,我把仓库留在这里:github.com/Gatoreno/DateTemplatePractice/blob/main/DataTemp/…
-
@E.Rawrdríguez.Ophanim 感谢分享,我已经更新了答案。有时间可以去看看。
标签: xamarin.forms mvvm data-binding datatemplate