【问题标题】:WPF: ComboBox SelectedIndex = -1?WPF:组合框 SelectedIndex = -1?
【发布时间】:2010-06-11 20:22:03
【问题描述】:

我正在使用包含多个页面的 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 { 得到 { 如果(_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);
        }
    }

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

【问题讨论】:

    标签: wpf combobox selectedindex


    【解决方案1】:

    将 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 事件。

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

    【讨论】:

    • 啊,现在我知道 -1 来自哪里,因为我没有在 XAML 中设置 SelectedIndex = 0,所以没有选择任何内容意味着如果没有选择任何内容,它的 -1 就像 DataGrids Rowhandle 一样是空的 :)使用 SelectedItem 和 SelectedIndex = 0 当我切换回一些页面时它可以正常工作,用户选择被保存!
    • 奇怪的是,当我有字符串属性时,您的解决方案不起作用,请参阅我更新的初始帖子!
    • 看看我的更新,我从你的代码开始修复了一些东西。
    • 你好 Zamboni,呃,这很奇怪,因为我的 SelectedLessonNumber 属性与 SelectedWeeklyRotationNumber 属于同一类。因此,两者都使用 propertychange 事件,并且两个组合框都使用相同的 datacontext/ViewModel ......
       数据模板> 
    • 该死的为什么 pre/code 标签在评论字段中不起作用?我已经看到其他用户在评论字段中使用这些标签并且语法被突出显示,你知道吗?
    猜你喜欢
    • 1970-01-01
    • 2014-03-30
    • 1970-01-01
    • 2010-12-03
    • 2017-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多