首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ComboBox SelectedIndex = -1?

ComboBox SelectedIndex = -1?
EN

Stack Overflow用户
提问于 2010-06-11 20:22:03
回答 1查看 9.2K关注 0票数 1

我使用的MVVM向导有几个页面。当我在组合框中设置一个值并转到下一页并切换回来时,我想要重置之前设置的值。

但是,所有发生的事情是,组合框在顶部是空的,索引是-1?

我做错什么了?

代码语言:javascript
复制
<ComboBox ItemsSource="{Binding Path=LessonNumbers}" SelectedIndex="{Binding SelectedLessonNumber}" />


 private ReadOnlyCollection<int> _lessonNumbers;
    public ReadOnlyCollection<int> LessonNumbers
    {
        get
        {
            if (_lessonNumbers == null)
                this.CreateLessonNumbers();

            return _lessonNumbers;
        }
    }

    private void CreateLessonNumbers()
    {
        var list = new List<int>();
        for (int i = 1; i < 24; i++)
        {
            list.Add(i);
        }

        _lessonNumbers = new ReadOnlyCollection<int>(list);
    }

    private int _selectedLessonNumber;
    public int SelectedLessonNumber 
    {
        get { return _selectedLessonNumber; }
        set
        {
            if (_selectedLessonNumber == value)
                return;

            _selectedLessonNumber = value;
            this.OnPropertyChanged("SelectedLessonNumber");
        }
    }

更新:

代码语言:javascript
复制
<ComboBox             
        SelectedIndex="0"
        SelectedItem="{Binding SelectedWeeklyRotationNumber}"
        ItemsSource="{Binding Path=WeeklyRotationNumbers}"
        Height="23" 
        HorizontalAlignment="Left"
        Margin="336,212,0,0"
        VerticalAlignment="Top"
        Width="121"
        MaxDropDownHeight="100"
        IsReadOnly="True"
        IsTextSearchEnabled="False"          
        />

ReadOnlyCollection _weeklyRotationNumbers;public ReadOnlyCollection WeeklyRotationNumbers { get { if (_weeklyRotationNumbers == null) this.CreateWeeklyRotationNumbers();

代码语言:javascript
复制
            return _weeklyRotationNumbers;
        }
    }

    private void CreateWeeklyRotationNumbers()
    {
        var list = new List<string>();

        list.Add("No rotation");
        for (int i = 1; i < 16; i++)
            list.Add(i.ToString());

        _weeklyRotationNumbers = new ReadOnlyCollection<string>(list);
    }

    private string _selectedWeeklyRotationNumber;
    public string SelectedWeeklyRotationNumber
    {
        get { return _selectedWeeklyRotationNumber; }
        set
        {
            if (_selectedWeeklyRotationNumber == value)
                return;

            _selectedWeeklyRotationNumber = value;
            this.RaisePropertyChanged("SelectedWeeklyRotationNumber");

            Messenger.Default.Send<string>(value);
        }
    }

同样,我错了什么或者字符串属性有什么问题?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-06-11 20:29:16

将XAML SelectedIndex更改为SelectedItem:

代码语言:javascript
复制
<ComboBox ItemsSource="{Binding Path=LessonNumbers}" 
          SelectedItem="{Binding SelectedLessonNumber}" /> 

更新:

在某个地方,必须设置窗口的DataContext以引用来自XAML的集合。

在我的例子中,我通常在视图的构造函数中这样做。

代码语言:javascript
复制
 // this my class containing WeeklyRotationNumbers
 private MainViewModel _mvm;

  public MainView()
  {
     InitializeComponent();

     _mvm = new MainViewModel();
     DataContext = _mvm;
  }

我将字符串添加到只读集合中:

代码语言:javascript
复制
  private ReadOnlyCollection<string> _weeklyRotationNumbers;
  public ReadOnlyCollection<string> WeeklyRotationNumbers

我还实现了接口INotifyPropertyChanged,但您可能使用不同的基类来处理PropertyChanged事件。

我从你的代码中剪下并粘贴了其他东西。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3025967

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档