【问题标题】:CollectionView, SelectedItems="{Binding SelectedElement, Mode=TwoWay}" Didn't workCollectionView, SelectedItems="{Binding SelectedElement, Mode=TwoWay}" 不起作用
【发布时间】: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


    【解决方案1】:

    在你的帮助下,我发现了我的问题。

    我不需要使用 SelectionChangedCommand(他工作但我不需要这个事件,我只需要数据)

    但尝试你的答案,我添加了这个:

    SelectionChangedCommandParameter="{Binding .}
    

    现在我的 IList SelectedElement 在我的视图中选择了数据!

    谢谢帮助我。

    【讨论】:

    • 小评论 - 也只是坚持这个问题。 SelectionChangedCommandParameter 对我来说不是必需的,只需将 ObservableCollection 更改为 IList 帮助
    【解决方案2】:

    需要进行两项更改:

    1. 绑定属性的类型 (SelectedElement) 必须与 CollectionView 属性的类型 (SelectedItems) 匹配。 XF Doc 表示类型为IList&lt;object&gt;

    2. 当绑定被解析(页面已加载;BindingContext 设置)时,您的绑定属性(如果类型正确)将设置为 SelectedItems 列表。它从一个空列表开始。随着选择的改变,列表的内容也会改变。 但列表对象本身是同一个对象,因此 setter 永远不会被再次调用。

    那么您如何看待变化?通过SelectionChangedCommandSelectionChanged event


    使用SelectionChangedCommand

    添加到 CollectionView XAML:

    <CollectionView ... SelectionChangedCommand="{Binding SelectionChangedCommand}" ...>
    

    添加到视图模型(您的 BindingContext):

        // There's no point in customizing this setter, because it won't get called when you want it to.
        public IList<object> SelectedElement { get; set; }
    
        // in constructor:
             SelectionChangedCommand = new Command(SelectionChanged);
    
        public Command SelectionChangedCommand { get; set; }
    
        private void SelectionChanged(object obj)
        {
            if (SelectedElement != null && SelectedElement.Count > 0) {
                var element = SelectedElement[0] as Element;
                ...
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-12
      • 1970-01-01
      • 2018-04-14
      • 1970-01-01
      相关资源
      最近更新 更多