我无法让任何绑定适用于SwipeItem
在一个RadListView
(这类似于标准ListView
)。特别是,我试图绑定到Command
财产;但是,我尝试绑定到其他属性,例如,Text
,但无济于事。
<telerikDataControls:RadListView ItemsSource ="{Binding Users, Mode=OneWay}">
<telerikDataControls:RadListView.ItemTemplate>
<DataTemplate>
<SwipeControl>
<SwipeControl.RightItems>
<SwipeItems>
<SwipeItem Text="Delete"
Background="Red"
Foreground="White"
Command="{Binding DeleteCommand}">
<SwipeItem.IconSource>
<SymbolIconSource Symbol="Delete"/>
</SwipeItem.IconSource>
</SwipeItem>
</SwipeItems>
</SwipeControl.RightItems>
<Grid>
<TextBlock Text="{Binding Name"/>
</Grid>
</SwipeControl>
</DataTemplate>
</telerikDataControls:RadListView.ItemTemplate>
</telerikDataControls:RadListView>
Users
在 ViewModel 的构造函数中设置;它是一个ObservableCollection
of UserViewModel
,每个属性都有我尝试使用的属性(通过 PropertyChanged 事件)。
The Name
装订工作在Grid
在模板的更下方,我已经检查了DataContext
of the SwipeControl
这是一个UserViewModel
。
在我的测试中我设置了一个Event
on the SwipeItem
:
<SwipeItem Text="Delete"
Background="Red"
Foreground="White"
Command="{Binding DeleteCommand}"
Invoked="SwipeItem_Invoked">
并在后面的代码中处理它:
private void SwipeItem_Invoked(SwipeItem sender, SwipeItemInvokedEventArgs args)
{
UserViewModel userToDelete = (UserViewModel)args.SwipeControl.DataContext;
}
我可以在这里看到sender.Command
is null
.
显然,快速的解决方案是使用事件模式;然而,我试图保持 MVVM 并尽可能避免代码隐藏。我以前从未遇到过与属性绑定的问题,所以想象一下我只是做了一些根本错误的事情。
Thanks.
Edit:
public class UserViewModel : MvxNotifyPropertyChanged // MvvmCross
{
public IMvxAsyncCommand DeleteCommand { get; }
private string _name;
public string Name // this property is bound in the Grid but will not bind in the SwipeItem
{
get => _name;
set => SetProperty(ref _name, value);
}
}