1

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

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

我错了什么?

<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");
        }
    }

更新:

<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();

            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);
        }
    }

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

4

1 回答 1

3

将 XAML SelectedIndex 更改为 SelectedItem:

<ComboBox ItemsSource="{Binding Path=LessonNumbers}" 
          SelectedItem="{Binding SelectedLessonNumber}" /> 

更新:

您必须在某处设置 Window 的 DataContext 以引用 XAML 中的集合。

在我的情况下,我通常在我的视图的构造函数中这样做。

 // this my class containing WeeklyRotationNumbers
 private MainViewModel _mvm;

  public MainView()
  {
     InitializeComponent();

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

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

  private ReadOnlyCollection<string> _weeklyRotationNumbers;
  public ReadOnlyCollection<string> WeeklyRotationNumbers

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

我从您的代码中剪切和粘贴的所有其他内容。

于 2010-06-11T20:29:16.073 回答