Question

I'm creating a windows phone app using MVVM light pattern. I'm having trouble on my list box as it always returning a negative value (-1) for selected index. Does anyone knows how to resolve it?

here is my code in View Model, do i missed anything?Thanks!

 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 code Here

 <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>
Was it helpful?

Solution

You are creating a new ListBox() (called lb) in your code. You don't populate it, so it will be empty and will always have a SelectedIndex of -1

Then check the 'Source' property of 'e' and cast it to a ListBox

ListBox myList = (ListBox) e.Source;

You can then access the properties on myList.

OTHER TIPS

based on my research SelectedIndex property of listbox is not bindable, when you use the get accessor for the SelectedIndex property, it always returns -1. An attempt to use the set accessor for the SelectedIndex property raises a NotSupportedException. -- MSDN List selectedporperty

I also updated my code since my first code is wrong that creates new listbox and that results to empty/null. Also selectionchanged event does not have a problem to be used as event.

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

Thats it. Hope it helps! :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top