【发布时间】:2019-05-02 18:46:35
【问题描述】:
我无法让任何绑定在RadListView(类似于标准ListView)中为SwipeItem 工作。特别是,我正在尝试绑定到 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的构造函数中为View设置的;它是UserViewModel 的ObservableCollection,每个都有我正在尝试使用的属性(带有PropertyChanged 事件)。
Name 绑定在模板下方的Grid 中有效,我检查了SwipeControl 的DataContext,它是UserViewModel。
在我的测试中,我在SwipeItem 上设置了一个Event:
<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 是null。
显然,快速的解决方案是使用事件模式;但是,我试图保留它 MVVM 并尽可能避免代码隐藏。我以前从未遇到过绑定到属性的问题,所以想象一下我只是在做一些根本错误的事情。
谢谢。
编辑:
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);
}
}
【问题讨论】:
-
看看这个关于命令绑定的问题。 stackoverflow.com/questions/12422945/…
-
DeleteCommand在UserViewModel类中吗?你能发布UserViewModel课程代码吗? -
@HirasawaYui 谢谢你的链接;但是,它是初学者级别的链接。我知道绑定是如何工作的——我一直在使用它们。不过,
SwipeItem及其属性的绑定似乎存在问题。 -
@MartinZikmund 我已根据您的要求更新了问题。
Name和DeleteCommand都在UserViewModel中。两者都不会绑定在SwipeItem;但是,它们将绑定在SwipeItem之外,例如,在Grid中进一步向下在DataTemplate中。我想SwipeItem有一些怪癖正在覆盖绑定甚至忽略它 - 我故意为SwipeItem进行了不正确的绑定并且没有出现警告。