XAML .NET Maui:ListView重新选择选定项

pobjuy32  于 2023-09-28  发布在  .NET
关注(0)|答案(1)|浏览(133)

我有.NET Maui应用程序,其中我显示ViewCells的ListView。当我选择一个ListView项目时,它将导航到另一个页面,当我从该页面导航回来时,我想要取消选择所选项目,或者我想要它,以便当我再次单击所选项目时,重新触发该设置事件
XAML:

<ListView ItemsSource="{Binding Keys}" SelectedItem="{Binding SelectedKey}">
              ...
      </ListView>

视图模型:

private Key _selectedKey;
        public Key SelectedKey
        {
            get => _selectedKey;
            set
            {
                SetProperty(ref _selectedKey, value);

                if (value != null && Shell.Current.IsLoaded) 
                {
                   SetProperty(ref _selectedKey, null);

                   Shell.Current.GoToAsync($"{nameof(EditKeyPage)}?Id={value.Id}");
                }
            }
        }
zengzsys

zengzsys1#

您可以在导航到另一个页面后将SelectedModel设置为null
请参考以下代码片段:

private Key _selectedModel;
        public  Key SelectedModel
        {
            get { return _selectedModel; }
            set
            {
                SetProperty(ref _selectedModel, value);

                if (SelectedModel != null)
                {  
                    Shell.Current.GoToAsync(nameof(DetailPage));
                    
                    // set the SelectedItem to null here
                    SelectedModel = null;
                }

            }
        }

我在我这边实现了这个功能,大家可以参考我的viewmodel的完整代码:

public class MyViewModel: INotifyPropertyChanged 
    {
        public ObservableCollection<Rayon> Sections { get; set; } = new ObservableCollection<Rayon>();

        private Rayon _selectedModel;
        public Rayon SelectedModel
        {
            get { return _selectedModel; }
            set
            {
                SetProperty(ref _selectedModel, value);

                if (SelectedModel != null)
                {

                    Shell.Current.GoToAsync(nameof(DetailPage));

                    SelectedModel = null;
                }

            }
        }

        public MyViewModel() {
            Sections.Add(new Rayon { SectionName= "section_1",Price=100.0});
            Sections.Add(new Rayon { SectionName= "section_2", Price = 100.0 });
            Sections.Add(new Rayon { SectionName= "section_3", Price = 100.0 });
    
        }
    
        bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
        {
            if (Object.Equals(storage, value))
                return false;
            storage = value;
            OnPropertyChanged(propertyName);
            return true;
        }
        protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
        public event PropertyChangedEventHandler PropertyChanged;

    }

注:

记住将Mode设置为TwoWay
用法示例:

<ListView ItemsSource="{Binding Sections}" HasUnevenRows="True" 
              SelectedItem="{Binding SelectedModel,Mode=TwoWay}"
              >

              <!--  other code -->
     </ListView>

相关问题