I have a report to fill in that is made up of a List
of sections, each of which has a List
of questions. My current implementation uses a Pivot
control whose ItemsSource
is set to the List
of sections and DataTemplate
is a LongListSelector
whose ItemsSource
is set to the List
of questions.
When the user attempts to submit the report, I loop through all the questions to check their validity. If any come back invalid, a flag is set and an error message is shown next to that question.
I would like to be able to focus on the first question that has that flag set but cannot figure out how to obtain a reference to the required LongListSelector
so that I can call it's ScrollTo
method.
Code:
View
<phone:Pivot toolkit:TurnstileFeatherEffect.FeatheringIndex="0"
Name="QuestionPivot"
ItemsSource="{Binding Sections, Mode=TwoWay}">
<phone:Pivot.Title>
<TextBlock>
<Run Text="{Binding Path=LocalizedResources.ApplicationTitle, Source={StaticResource LocalizedStrings}}"/>
<Run Text=" "/>
<Run Text="{Binding Path=LocalizedResources.DailyReport, Source={StaticResource LocalizedStrings}}"/>
<Run Text=" - "/>
<Run Text="{Binding ProjectName}"/>
</TextBlock>
</phone:Pivot.Title>
<phone:Pivot.HeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding SectionName}"/>
</DataTemplate>
</phone:Pivot.HeaderTemplate>
<phone:Pivot.ItemTemplate>
<DataTemplate>
<phone:LongListSelector ItemsSource="{Binding Questions, Mode=TwoWay}">
<phone:LongListSelector.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0 0 0 20">
...
</StackPanel>
</DataTemplate>
</phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>
</DataTemplate>
</phone:Pivot.ItemTemplate>
</phone:Pivot>
Code Behind (including my failed attempt to cast the SelectedItem to a LongListSelector...)
private void FocusOnFirstInvalid(object sender, EventArgs e)
{
int selectedItem = 1;
foreach (ReportDraftSection s in QuestionPivot.Items)
{
foreach(ReportQuestion q in s.Questions)
{
if(q.IsNotValid)
{
QuestionPivot.SelectedIndex = selectedItem;
var l = (LongListSelector)QuestionPivot.SelectedItem;
l.ScrollTo(q);
}
}
selectedItem += 1;
}
}
Any help would be greatly appreciated.
Since Pivot's ItemsSource
is a List of Section
, QuestionPivot.SelectedItem
contains a Section
object instead of LongListSelector. You can try to use ItemContainerGenerator
to get PivotItem from SelectedItem
, then get LongListSelector from PivotItem's Content
(not tested yet) :
.......
QuestionPivot.SelectedIndex = selectedItem;
var pivotItem = (PivotItem)QuestionPivot
.ItemContainerGenerator
.ContainerFromItem(QuestionPivot.SelectedItem);
var l = (LongListSelector)pivotItem.Content;
l.ScrollTo(q);
.......