WPF:组合框选定索引 = -1?

发布于 2024-09-05 07:20:43 字数 2717 浏览 1 评论 0原文

我正在使用具有多个页面的 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; 公共 ReadOnlyCollection WeeklyRotationNumbers { 得到 { 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);
        }
    }

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

I am using a MVVM Wizard with several pages. When I set a value in the combobox and go to the next page and switch back I want to reset the value I set before.

But all whats happening is that the combobox is empty at top and the index is -1 ?

What do I wrong?

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

UPDATE:

<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"          
        />

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

Again, what do I wrong or what is wrong with the string property?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

已下线请稍等 2024-09-12 07:20:43

将 XAML SelectedIndex 更改为 SelectedItem:

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

更新:

您必须在某个地方设置窗口的 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 事件。

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

Change XAML SelectedIndex to SelectedItem:

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

UPDATE:

Somewhere you must set the DataContext of your Window to reference the collection from your XAML.

In my case I typically do that in the constructor of my view.

 // this my class containing WeeklyRotationNumbers
 private MainViewModel _mvm;

  public MainView()
  {
     InitializeComponent();

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

I added string to the read only collections:

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

I also implemented the interface INotifyPropertyChanged which I think you did, but you are likely using
a different base class to handle the PropertyChanged event.

Everthing else I cut and paste from your code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文