【问题标题】:ListBox Index returns negative value列表框索引返回负值
【发布时间】:2013-04-19 02:57:23
【问题描述】:

我正在使用 MVVM 光模式创建一个 Windows Phone 应用程序。我的列表框有问题,因为它总是为选定的索引返回负值 (-1)。有谁知道如何解决它?

这是我在视图模型中的代码,我错过了什么吗?谢谢!

 public void OnViewListSelectedItem(SelectionChangedEventArgs e)
    {

        ListBox lb = new ListBox();

        if (e.AddedItems.Count == 1)
        {
            if (lb.SelectedIndex == 0)
            {
                _navigationService.NavigateTo(new Uri(ViewModelLocator.ByVendorUrl, UriKind.Relative));
            }

            if (lb.SelectedIndex == 1)
                {
                    _navigationService.NavigateTo(new Uri(ViewModelLocator.ByVendorUrl, UriKind.Relative));
                }
            if (lb.SelectedIndex == 2)
                {
                    _navigationService.NavigateTo(new Uri(ViewModelLocator.ByCombinationUrl, UriKind.Relative));
                }
            }
    }

XAML 代码在这里

 <ListBox x:Name="lbviewlist">
                <i:Interaction.Triggers>
                        <i:EventTrigger EventName="SelectionChanged">
                            <Command:EventToCommand Command="{Binding ViewListCommand}" PassEventArgsToCommand="True"/>
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                    <ListBox.Items>
                    <ListBoxItem Content="By Product" FontSize="35" Margin="10,12,12,0"/>
                    <ListBoxItem Content="By Vendor" FontSize="35" Margin="10,12,12,0"/>
                    <ListBoxItem Content="By Best Combination" FontSize="35" Margin="10,12,12,0"/>
                    </ListBox.Items>
                </ListBox>

【问题讨论】:

    标签: windows-phone-7 mvvm listbox listboxitem selectedindex


    【解决方案1】:

    您正在代码中创建一个新的 ListBox()(称为 lb)。你不填充它,所以它会是空的并且总是有一个 -1 的 SelectedIndex

    然后检查'e'的'Source'属性并将其转换为ListBox

    ListBox myList = (ListBox) e.Source;
    

    然后您可以访问 myList 上的属性。

    【讨论】:

    • 谢谢,我刚刚意识到创建新列表框是错误的。但是,您知道如何检查 lblistview 的选择状态吗?我不知道如何从我的代码中访问/ lblistview。唯一可访问的是 (e)。你能帮助我吗?请。非常感谢
    • 你在 XAML 中命名了它,所以你可以从它的名字 (lbviewlist) 中访问它。你会看到可用的属性、事件和方法。
    • 我无法从它的名称访问它,因为我不是从后面的代码编码,而是使用 View Model。访问它的唯一方法是通过我的方法 ViewListCommand 的 (e) 参数,该方法绑定到我的列表框的事件。但是,当我在 ViewModel 中直接输入 (e) 时,唯一可用的选项是 AdditionalItems,如我上面的代码所示。
    • 哦,我明白了..e 应该有一个指向控件的“源”属性:msdn.microsoft.com/en-us/library/…
    • 没问题...见我上面的回答:) ListBox myList = (ListBox) e.Source;
    【解决方案2】:

    根据我的研究,列表框的 SelectedIndex 属性不可绑定,当您对 SelectedIndex 属性使用 get 访问器时,它总是返回 -1。尝试对 SelectedIndex 属性使用 set 访问器会引发 NotSupportedException。 -- MSDN List selectedporperty

    我还更新了我的代码,因为我的第一个代码是错误的,它创建了新的列表框并导致为空/null。另外 selectionchanged 事件作为事件使用也没有问题。

        public void method (SelectionChangedEventArgs e)
        {
    
    
            {                
                if (e.AddedItems.Count == 1)
                {
                    var listBoxItem = (ListBoxItem)e.AddedItems[0];
                    string _string1 = "Test";
                    if ((string)listBoxItem.Content == _string1)
                    {
                        navigationService.NavigateTo(new Uri(ViewModelLocator.page1, UriKind.Relative));
                    }
                }
             }
    }
    

    就是这样。希望能帮助到你! :)

    【讨论】:

      猜你喜欢
      • 2017-09-17
      • 2021-12-17
      • 2019-08-01
      • 1970-01-01
      • 2016-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多