簡體   English   中英

在ScrollViewer控件中無法進入“ ScrollStates”

[英]Unable to get to “ScrollStates” in ScrollViewer Control

我在嘗試使其工作方面遇到很多麻煩,希望有人可以提供幫助。

我的WindowsPhone應用程序中有一個ScrollViewer,並且我試圖模擬與您在本機Calendar應用程序中看到的“日期/時間選擇器”類似的控件。 因此,我的ScrollViewer包含一個StackPanel,其中包含多個帶有矩形和TextBlocks的方形Canvas。 我的目的是觀看“ ScrollStates”,然后在VisualState更改為“ NotScrolling”時,然后檢查ScrollViewer的VerticalOffset並將幻燈片動畫設置為最接近的“ snap-to”位置(即,將正方形對齊到正確/中間位置)。

<ScrollViewer Name="sv" Width="100" VerticalScrollBarVisibility="Hidden" HorizontalScrollBarVisibility="Disabled" Loaded="ScrollViewer_Loaded">
    <StackPanel>
        <Canvas MaxWidth="77" MaxHeight="80" MinWidth="80" MinHeight="80" Margin="3">
            <Rectangle Stroke="{StaticResource PhoneForegroundBrush}" StrokeThickness="3" Width="80" Height="80" />
            <TextBlock Text="1" FontSize="36" FontWeight="Bold" TextAlignment="Center" HorizontalAlignment="Center" Width="70" Canvas.Left="6" Canvas.Top="14" LineHeight="48" />
        </Canvas>
        <Canvas MaxWidth="77" MaxHeight="80" MinWidth="80" MinHeight="80" Margin="3">
            <Rectangle Stroke="{StaticResource PhoneForegroundBrush}" StrokeThickness="3" Width="80" Height="80" />
            <TextBlock Text="2" FontSize="36" FontWeight="Bold" TextAlignment="Center" HorizontalAlignment="Center" Width="70" Canvas.Left="6" Canvas.Top="14" LineHeight="48" />
        </Canvas>
        <Canvas MaxWidth="77" MaxHeight="80" MinWidth="80" MinHeight="80" Margin="3">
            <Rectangle Stroke="{StaticResource PhoneForegroundBrush}" StrokeThickness="3" Width="80" Height="80" />
            <TextBlock Text="3" FontSize="36" FontWeight="Bold" TextAlignment="Center" HorizontalAlignment="Center" Width="70" Canvas.Left="6" Canvas.Top="14" LineHeight="48" />
        </Canvas>
        ...
    </StackPanel>
</ScrollViewer>

我一直在查看與VisualStates掛鈎的各種示例,例如http://blogs.msdn.com/b/ptorr/archive/2010/07/23/how-to-detect-when-a-list-is -scrolling-or-not.aspx ; http://developingfor.net/2009/02/16/fun-with-the-wpf-scrollviewer/ ; http://blogs.msdn.com/b/slmperf/archive/2011/06/30/windows-phone-mango-change-listbox-how-to-detect-compression-end-of-scroll-states.aspx 。 ..所有人似乎都與此類似

// Visual States are always on the first child of the control template
FrameworkElement element = VisualTreeHelper.GetChild(sv, 0) as FrameworkElement; 

...然后繼續尋找VisualStateGroup group = FindVisualState(element, "ScrollStates"); ,當事件發生變化時,他們可以從中掛鈎事件。

但是...每當我嘗試將VisualTreeHelper.GetChild(sv,0) as FrameworkElement ,應用程序都會崩潰,但類型為'System.ArgumentOutOfRangeException'的異常除外。 如果我輸出VisualTreeHelper.GetChildrenCount(sv) ,則始終為“ 0”。 看來對其他所有人有效嗎? 8)

任何幫助將不勝感激。 謝謝!

(作為替代方案,是否有人在我可以使用的可重用控件中制作了這種“選擇框”,而不是嘗試重新創建它?)

您是否一直等到scrollviewer的Loaded事件觸發后才嘗試獲取scrollviewer的子級?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM