【问题标题】:Listbox First Item Selected in mvvm在 mvvm 中选择的列表框第一项
【发布时间】:2012-10-28 11:36:58
【问题描述】:

我是 mvvm 的新手。我的 silverlight 应用程序中有一个列表框,它绑定到视图模型中的一个可观察集合,我想制作列表框并选择第一个项目。我厌倦了这个,但它不起作用。

<ListBox Height="431" Canvas.Left="17" Canvas.Top="77" Width="215" FontSize="13" ItemsSource="{Binding Path=Categorys, Mode=TwoWay}" DataContext="{Binding}" SelectedItem="{Binding CurrentCategory, Mode=TwoWay}" ItemTemplate="{StaticResource CategoryDataTemplate}" ScrollViewer.HorizontalScrollBarVisibility="Disabled" Name="lst_category">

然后我在主页视图模型的主页加载中添加了这个

CurrentCategory = Categorys[0];

谁能帮帮我

【问题讨论】:

  • 你试过SelectedIndex吗?
  • 你也应该尝试这种方式 CurrentCategory = Categorys.FirstOrDefault()

标签: c# xaml mvvm silverlight-4.0


【解决方案1】:

执行以下步骤:

  1. 确保集合Categorys 已填满。您可能需要使用AsycCTP, Asynchronous Programming with Async and Await 或其他一些机制来首先等待集合被填充。

    await 运算符应用于异步方法中的任务,以暂停该方法的执行,直到等待的任务完成。任务代表正在进行的工作。

  2. 在 ViewModel 中实现 INotifyPropertyChanged,公开 PropertyCurrentCategory 并从 PropertySetter 中引发 PropertyChanged 事件。

    private Category _currentCategory = null;
    
    public Category CurrentCategory 
    {
        get { return _currentCategory; }
        set
        {
            if (_currentCategory != value)
            { 
                _currentCategory = value;
    
                // Update bindings
                RaisePropertyChanged("CurrentCategory");
            }
        }
    }
    

现在您可以使用相同的代码:

CurrentCategory = Categorys[0];

【讨论】:

  • 我已经尝试过了,但我的 Categorys 集合需要时间从服务加载并且具有空值。
  • 所以你应该使用 AsychCTP await 或其他一些机制来首先等待集合填满。
【解决方案2】:

尝试使用ICollectionViewIsSynchronizedWithCurrentItem。 CollectionView 具有您需要的所有功能。例如MoveToFirst()

Xaml:

<ListBox ItemsSource="{Binding Categories}" 
                 DisplayMemberPath="Name" 
                 IsSynchronizedWithCurrentItem="True" />

视图模型:

 public class ViewModel :INotifyPropertyChanged
        {
            private ObservableCollection<Category> _categories = new ObservableCollection<Category>();
            private Category _currentCategory;

            public ObservableCollection<Category> Categories
            {
                get { return _categories; }
                set { _categories = value; OnPropertyChanged("Categories");}
            }

            public Category CurrentCategory
            {
                get { return _currentCategory; }
                set { _currentCategory = value; OnPropertyChanged("CurrentCategory");}
            }

            public ICollectionView CategoriesView { get; private set; }

            public ViewModel()
            {
                Categories.Add(new Category{Id = Guid.NewGuid(), Name = "Cat1"});
                Categories.Add(new Category{Id = Guid.NewGuid(), Name = "Cat2"});
                Categories.Add(new Category{Id = Guid.NewGuid(), Name = "Cat3"});

                CategoriesView = CollectionViewSource.GetDefaultView(Categories);
                CategoriesView.CurrentChanged += OnCategoriesChanged;
                CategoriesView.MoveCurrentToFirst();
            }

            private void OnCategoriesChanged(object sender, EventArgs e)
            {
                var selectedCategory = CategoriesView.CurrentItem as Category;
                if (selectedCategory == null) return;

                CurrentCategory = selectedCategory;
            }

            public event PropertyChangedEventHandler PropertyChanged;

            protected virtual void OnPropertyChanged(string propertyName)
            {
                PropertyChangedEventHandler handler = PropertyChanged;
                if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public class Category
        {
            public Guid Id { get; set; }

            public string Name { get; set; }
        }

【讨论】:

    【解决方案3】:

    你也应该尝试这种方式......

    列表 c = 新列表

    CurrentCategory = c.firstOrDefault()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-24
      • 2015-11-04
      • 2018-04-01
      • 2012-03-21
      • 1970-01-01
      相关资源
      最近更新 更多