Lester の WPF\SL ブログの記事Determining the Visibility of Elements inside ScrollViewerのメソッドを使用して、特定のアイテムが に表示されているかどうかを判断ScrollViewerできます。そのブログから:
// position of your visual inside the scrollviewer
GeneralTransform childTransform = ContainedObject.TransformToAncestor(ScrollViewerObj);
Rect rectangle = childTransform.TransformBounds(new Rect(new Point(0,0),
ContainedObject.RenderSize));
// Check if the elements Rect intersects with that of the scrollviewer's
Rect result = Rect.Intersect(new Rect(new Point(0, 0), ScrollViewerObj.RenderSize),
rectangle);
// if result is Empty then the element is not in view
if (result == Rect.Empty)
{
ContainedObject.IsPopupOpen = false; // <<< Close relevant popup here
}
else
{
//obj is partially Or completely visible
//skip or bring obj in view.
}
bool IsPopupOpen明らかに、これを機能させるには、プロパティにデータ バインドされたデータ オブジェクトに新しいプロパティを追加する必要がありTextBox.Textます。次に、プロパティにもBindこれを行う必要があります。Popup.IsOpen
<Popup IsOpen="{Binding IsPopupOpen}" StaysOpen="False" ... />
更新 >>>
がスクロールされると、ScrollViewer.ScrollChangedイベントが頻繁に呼び出されます。ScrollViewerそのイベント ハンドラーでコードを直接実行するのは賢明ではありませんが、ScrollChangedEventArgsオブジェクトには役立つプロパティがいくつかあります。これらのプロパティの詳細については、MSDNのScrollChangedEventArgsクラスページを参照してください。
という名前のイベント ハンドラーでアクセスできる 2 つのプロパティがありVerticalChange、最後のイベント以降にスクロールされたHorizontalChange量を示す必要があります。ScrollViewer[かなり前のことなので、それらが正しいプロパティであることを保証することはできませんが、MSDN のScrollChangedEventArgs.VerticalChangeプロパティページのサンプル コードをイベント ハンドラーで使用すると、読み上げによってどれが正しいかの手がかりが得られるはずです。使用する正しいプロパティ]
したがって、関連するプロパティを見つけたら、ハンドラーでそれらを使用して、コードを実行するかどうかを決定できます...次のようなことを試してください。
double totalVerticalChange = 0.0;
double minimumValue = 24.0; // set this to whatever you want as a minimum scroll value
...
private void ScrollViewer_Changed(object sender, ScrollChangedEventArgs e)
{
if (totalVerticalChange + e.VerticalChange >= minimumValue)
{
totalVerticalChange = 0.0;
// perform your functionality here
}
else totalVerticalChange += e.VerticalChange;
}
現時点では Visual Studio でこれを確認できないため、ここにエラーがある場合はご容赦ください。