locked
C# - Windows Store app 8.1 ListView defaults to 2nd item

    Question

  • So I have 2 listview. The main one contains the hours of the day the other has the day of the week.

    If I select an item from the hours listView it easily selects the right day but when I put the code in for selecting the right hour from the selected day it goes wrong. The first option(Today) will selected the correct hour, the next one (tomorrow) will also be selected fine. But when I try any of the days after tomorrow it goes wrong. I have debugged it and found that it runs through the method fine and selects the right hour but then the method runs again and defaults the selection back to the second option(Tomorrow). That happens no matter what other day I click on(Excluding Today).

    This is the C# code I used for selecting the day

    switch (listView.SelectedIndex)
    {
        case 0:
            gridPanels.SelectedIndex = hourIndex[0];
            break;
        case 1:
            gridPanels.SelectedIndex = hourIndex[1];
            break;
        case 2:
            gridPanels.SelectedIndex = hourIndex[2];
            break;
        case 3:
            gridPanels.SelectedIndex = hourIndex[3];
            break;
        case 4:
            gridPanels.SelectedIndex = hourIndex[4];
            break;
    }

    // In the XAML Im using SelectionChanged="Method_Name"

    Note: I have checked via the debugger that my hourIndex[] has the correct values I want BUT the method runs again and selects the second value. 
    Other Note: I posted on Stackoverflow and one person responded saying I should be using a viewModel which didnt seem right to me since all the tutorials I've used havent used one. 


    ///EDIT: Ive tried inputting straight int values into the switch/case. Once I go over somewhere around 20-30 it will do the above problem
    • Edited by blackasninja Thursday, February 13, 2014 7:54 PM New info
    Thursday, February 13, 2014 9:05 AM

All replies

  • You're doing something which causes the method to be called twice, and should figure out why its doing that.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Thursday, February 13, 2014 1:33 PM
    Moderator
  • The method is being called by selection_changed method for the listView
    Thursday, February 13, 2014 7:58 PM
  • Can you post your project?

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Thursday, February 13, 2014 8:36 PM
    Moderator
  • This is for a work project so I cant post the whole thing here. 
    If I comment out the code snippet the other sections of code run fine(Clicking an hour will select the correct day).
    Also my new edit anything above 20 seems to default back to 20 which is odd cause I dont think I've set it anywhere else in my code 
    Thursday, February 13, 2014 8:56 PM
  • Can you post a project that reproduces the exact problem you're having? When you're writing it,  I think you'll either figure out why your method is being called twice, or not be able to reproduce the problem at all...

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Friday, February 14, 2014 1:16 PM
    Moderator