【发布时间】:2021-11-05 13:56:47
【问题描述】:
我正在尝试在我的 View 和我的 ViewModel 之间绑定我的 CollectionView 的“SelectedItems”属性。但是 TwoWay 没有用。
我可以向我的视图发送信息,但我不能向我的视图模型发送信息。
(所有其他投标都正常工作)
一些代码,在我的 ViewModel 中:
private ObservableCollection<Element> _selectedElement;
public ObservableCollection<Element> SelectedElement
{
get
{
return _selectedElement;
}
set
{
if (_selectedElement != value)
{
_selectedElement = value;
}
}
}
在我看来
<CollectionView ItemsSource="{Binding Elements,Mode=TwoWay}"
SelectionMode="Multiple"
SelectedItems="{Binding SelectedElement, Mode=TwoWay}">
<CollectionView.ItemTemplate>
<DataTemplate>
<StackLayout Orientation="Horizontal" >
<StackLayout Padding="15,10" Orientation="Horizontal" >
<Label Text="{Binding Libelle}" VerticalOptions="Center" />
<Label Text="{Binding Code}" VerticalOptions="Center" />
</StackLayout>
<StackLayout HorizontalOptions="EndAndExpand" VerticalOptions="Center" >
<ImageButton Source="{Binding ImgFavori}"
BackgroundColor="Transparent"
Command="{Binding Path=BindingContext.ManageFavCommand, Source={x:Reference CurrentView}}"
CommandParameter="{Binding .}"
WidthRequest="75"
VerticalOptions="Fill"/>
</StackLayout>
</StackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
当我使用 ViewModel 像这样操作视图时:
private async void ClearCollection()
{
SelectedElement = null;
}
这是工作。
但是当我尝试在智能手机上选择某行时,我从不传入 SelectedElement 的设置器。
我正在使用 Xamarin.Forms 5.0.0.2196
谢谢帮助我。
【问题讨论】:
标签: c# xamarin.forms mvvm-light