Click here to Skip to main content
15,881,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have 4 subjects. Each subject has 500 questions. Once program loads settings it will give you an option to choose subject. Once user selects a subject it will load the list of questions. If question is solved then it is shown as green.

I am trying to scroll to unsolved questions automatically.

ListView in xaml

XML
<ListView x:Name="LstQuestions" SelectionMode="None" Grid.Row="1" d:IsLocked="True">
    <ListView.ItemsPanel>
         <ItemsPanelTemplate>
             <WrapGrid Orientation="Horizontal"/>
         </ItemsPanelTemplate>
    </ListView.ItemsPanel>
</ListView>


ListView items are loaded from code.

C#
InitializeComponent();

for(int i=1;i<= 500; i++)
{
     Button btn = new Button();
     btn.Width = 120;
     btn.Height = 120;
     btn.Click += Btn_Click;
     btn.Content = i;
     LstQuestions.Items.Add(btn);
}

LstQuestions.UpdateLayout();

LstQuestions.ScrollIntoView((Button)LstQuestions.Items[_CurrentQuestionNumber]);


How can I achieve it.?

What I have tried:

I searched in Google and found many methods but none of them worked. Also downloaded WinrtXamlToolkit. It provides extended methods for scrolling. This also didn't work. Tried semanticzoomlocation too. So far no results.
Posted

1 solution

I don't know what the problem was. But I found out that if you call ScrollIntoView from an event it will work.

So I created Layout_Updated event. Called the ScrollIntoView from that event.

XML
<ListView x:Name="LstQuestions" SelectionMode="None" Grid.Row="1" LayoutUpdated="LstQuestions_LayoutUpdated" d:IsLocked="True">
    <ListView.ItemsPanel>
         <ItemsPanelTemplate>
             <WrapGrid Orientation="Horizontal"/>
         </ItemsPanelTemplate>
    </ListView.ItemsPanel>
</ListView>



C#
public Questions()
{  
    InitializeComponent();
    for(int i=1;i<= 500; i++)
    {
         Button btn = new Button();
         btn.Width = 120;
         btn.Height = 120;
         btn.Click += Btn_Click;
         btn.Content = i;
         LstQuestions.Items.Add(btn);
    }
    LstQuestions.UpdateLayout();
}



C#
private void LstQuestions_LayoutUpdated(object sender, object e)
{
   LstQuestions.ScrollIntoView(LstQuestions.Items[_CurrentQuestionNumber]);
}




Now it works.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900