Created
November 27, 2012 03:22
-
-
Save biac/4152160 to your computer and use it in GitHub Desktop.
GridViewのスクロール位置を復元するには
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- GridView を ScrollViewer に埋め込んでやる。GridView のサイズは、その中身だけで決まるようになる。 --> | |
<ScrollViewer x:Name="ScrollViewer1" Grid.Row="1" | |
HorizontalScrollMode="Enabled" HorizontalScrollBarVisibility="Auto" | |
VerticalScrollMode="Disabled" VerticalScrollBarVisibility="Disabled" > | |
<GridView x:Name="GridView1" Margin="120,0,0,0" SizeChanged="GridView1_SizeChanged" | |
SelectedIndex="-1" | |
ItemsSource="{Binding Source={StaticResource definedColorsSource}}" | |
ItemTemplate="{StaticResource ColorInfoDataTemplate150x150}" /> | |
</ScrollViewer> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// コードビハインドはこんな感じで。 | |
// Metro Color Chart http://apps.microsoft.com/webpdp/ja-jp/app/bwdevtools-metro-color-chart/c19ab4ed-54e8-4799-85cb-3659ca12c915 | |
// …で実装した方法なんだけど、かなりゴチャついてるので骨組みだけリライト。 | |
private void GridView1_SizeChanged(object sender, SizeChangedEventArgs e) | |
{ | |
if (! _is復元データのバインド済み) | |
return; | |
ScrollViewer1.ScrollToHorizontalOffset(復元するスクロール位置); | |
_is復元データのバインド済み = false; | |
} | |
// LoadState()内で GridView のデータを復元してバインドしたら、 _is復元データのバインド済み フラグを立ててやる。 | |
// GridView はバインドし終えると自分のサイズを変えるので、GridView1_SizeChanged イベントハンドラーが呼ばれる。そこで、データを復元した直後のイベントかどうか判定して、復元時なら ScrollViewer をスクロールさせてやる。 |
でも、ScrollViewerの中にGridViewを埋め込むと、GridViewのUI仮想化が効かなくなるんだよね。つまり、表示が出るまでが、遅いorz
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
LoadState()内で GridView のデータを復元してバインドしたら、 _is復元データのバインド済み フラグを立ててやる。
GridView はバインドし終えると自分のサイズを変えるので、GridView1_SizeChanged イベントハンドラーが呼ばれる。そこで、データを復元した直後のイベントかどうか判定して、復元時なら ScrollViewer をスクロールさせてやる。