FrameworkElement.BringIntoView 方法

定义

尝试将此元素放入其所在的任何可滚动区域内的视图中。Attempts to bring this element into view, within any scrollable regions it is contained within.

重载

BringIntoView(Rect)

尝试将此元素的提供区域大小放入其所在的任何可滚动区域内的视图中。Attempts to bring the provided region size of this element into view, within any scrollable regions it is contained within.

BringIntoView()

尝试将此元素放入其所在的任何可滚动区域内的视图中。Attempts to bring this element into view, within any scrollable regions it is contained within.

BringIntoView(Rect)

尝试将此元素的提供区域大小放入其所在的任何可滚动区域内的视图中。Attempts to bring the provided region size of this element into view, within any scrollable regions it is contained within.

public:
 void BringIntoView(System::Windows::Rect targetRectangle);
public void BringIntoView (System.Windows.Rect targetRectangle);
member this.BringIntoView : System.Windows.Rect -> unit
Public Sub BringIntoView (targetRectangle As Rect)

参数

targetRectangle
Rect

也应放入视图中的元素的指定大小。Specified size of the element that should also be brought into view.

示例

下面的示例在受约束的滚动区域中有一个大型图形。The following example has a large graphic in a constrained scrolling region. 页面上的按钮具有处理程序,可将视图滚动到大型图形的特定区域。A button on the page has a handler that scrolls the view to a particular region of the large graphic.

<ScrollViewer Width="300" Height="300" HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Visible">
  <Image Name="mapframe" ScrollViewer.CanContentScroll="True"  >
    <Image.Source>
      <BitmapImage UriSource="treasuremap.bmp"/>
    </Image.Source>
  </Image>
</ScrollViewer>
<StackPanel>
  <Button Click="GoToLake">Go to Lake</Button>

</StackPanel>
void GoToLake(object sender, RoutedEventArgs e)
{
    mapframe.BringIntoView(new Rect(800, 400, 200, 200));
}
Private Sub GoToLake(ByVal sender As Object, ByVal e As RoutedEventArgs)
    mapframe.BringIntoView(New Rect(800, 400, 200, 200))
End Sub

注解

通过调用此方法,可以引发源自当前元素的 RequestBringIntoView 事件。By calling this method, you raise a RequestBringIntoView event that originates from the current element. 引发此事件的目的是 ScrollViewer或派生类或类似类处理此事件。This event is raised so that it can be handled by a ScrollViewer, or a derived or similar class. 预期的行为是由父元素处理事件(在事件数据中标记为已处理),事件源通过嵌入在 ScrollViewer 控件中的逻辑进入视图。The expected behavior is that the event is handled by the parent element, marked handled in the event data, and the source of the event is brought into view through the logic embedded in the ScrollViewer control. RequestBringIntoView 事件和 BringIntoView 方法均不传输任何有关成功或失败的信息,而不是该事件通常标记为成功处理。Neither the RequestBringIntoView event nor the BringIntoView method transmit any information about success or failure, other than that the event is typically marked handled on success. 失败的原因可能包括元素设置,如 VisibilityVisible以外的其他值。Reasons for failure can include the element settings, such as Visibility being some value other than Visible.

如果使用未指定 targetRectangle的签名,则整个元素大小(其 RenderSize)将变得可见。If you use the signature that does not specify a targetRectangle, then the entire element size (its RenderSize) will be made visible.

通过调用此方法,您可能会对包含该元素的任何父可滚动区域调用 MakeVisibleBy calling this method, you potentially will call MakeVisible on any parent scrollable area that contains the element. 如果此元素不包含在可滚动区域中,则仍会引发 RequestBringIntoView 事件,但由于没有事件侦听器,因此将不起作用。If this element is not contained in a scrollable area, the RequestBringIntoView event is still raised, but there will be no effect because there are no event listeners.

另请参阅

BringIntoView()

尝试将此元素放入其所在的任何可滚动区域内的视图中。Attempts to bring this element into view, within any scrollable regions it is contained within.

public:
 void BringIntoView();
public void BringIntoView ();
member this.BringIntoView : unit -> unit
Public Sub BringIntoView ()

示例

下面的示例为应用程序导航事件实现了一个处理程序,该事件在导航到的统一资源标识符(URI)包含片段时做出响应。The following example implements a handler for an application navigation event that responds whenever the uniform resource identifier (URI) being navigated to includes a fragment. 片段在哈希符号(#)后面的 URI 中命名,实现的行为使元素滚动到帧内的视图中。The fragment is named in the URI following the hash sign (#), and the implemented behavior causes the element to scroll into view within the frame. BringIntoViewRequestBringIntoView 请求该示例中的滚动行为。BringIntoView and RequestBringIntoView request that scrolling behavior in the example.

void browserFrame_FragmentNavigation(object sender, FragmentNavigationEventArgs e)
{
    object content = ((ContentControl)e.Navigator).Content;
    FrameworkElement fragmentElement = LogicalTreeHelper.FindLogicalNode((DependencyObject)content, e.Fragment) as FrameworkElement;
    if (fragmentElement == null)
    {
        // Redirect to error page
        // Note - You can't navigate from within a FragmentNavigation event handler,
        //        hence creation of an async dispatcher work item
        this.Dispatcher.BeginInvoke(
            DispatcherPriority.Send,
            (DispatcherOperationCallback) delegate(object unused) 
            {
                this.browserFrame.Navigate(new Uri("FragmentNotFoundPage.xaml", UriKind.Relative));
                return null;
            },
            null);
        e.Handled = true;
    }
}
Private Sub browserFrame_FragmentNavigation(ByVal sender As Object, ByVal e As FragmentNavigationEventArgs)
    Dim element As FrameworkElement = TryCast(LogicalTreeHelper.FindLogicalNode(DirectCast(DirectCast(e.Navigator, ContentControl).Content, DependencyObject), e.Fragment), FrameworkElement)
    If (element Is Nothing) Then
        ' Redirect to error page
        ' Note - You can't navigate from within a FragmentNavigation event handler,
        '        hence creation of an async dispatcher work item
        Dim callback As New DispatcherOperationCallback(AddressOf Me.FragmentNotFoundNavigationRedirect)
        Me.Dispatcher.BeginInvoke(DispatcherPriority.Normal, callback, Nothing)
    End If
    e.Handled = True
End Sub

Function FragmentNotFoundNavigationRedirect(ByVal unused As Object) As Object
    Me.browserFrame.Navigate(New Uri("FragmentNotFoundPage.xaml", UriKind.Relative))
    Return Nothing
End Function

注解

通过调用此方法,可以引发源自当前元素的 RequestBringIntoView 事件。By calling this method, you raise a RequestBringIntoView event that originates from the current element. 引发此事件的目的是 ScrollViewer或派生类或类似类处理此事件。This event is raised so that it can be handled by a ScrollViewer, or a derived or similar class. 预期的行为是由父元素处理事件(在事件数据中标记为已处理),事件源通过嵌入在 ScrollViewer 控件中的逻辑进入视图。The expected behavior is that the event is handled by the parent element, marked handled in the event data, and the source of the event is brought into view through the logic embedded in the ScrollViewer control. RequestBringIntoView 事件和 BringIntoView 方法均不传输任何有关成功或失败的信息,而不是该事件通常标记为成功处理。Neither the RequestBringIntoView event nor the BringIntoView method transmit any information about success or failure, other than that the event is typically marked handled on success. 失败的原因可能包括元素设置,如 VisibilityVisible以外的其他值。Reasons for failure can include the element settings, such as Visibility being some value other than Visible.

如果使用未指定 targetRectangle的签名,则整个元素大小(其 RenderSize)将变得可见。If you use the signature that does not specify a targetRectangle, then the entire element size (its RenderSize) will be made visible.

通过调用此方法,您可能会对包含该元素的任何父可滚动区域调用 MakeVisibleBy calling this method, you potentially will call MakeVisible on any parent scrollable area that contains the element. 如果此元素不包含在可滚动区域中,则仍会引发 RequestBringIntoView 事件,但由于没有事件侦听器,因此将不起作用。If this element is not contained in a scrollable area, the RequestBringIntoView event is still raised, but there will be no effect because there are no event listeners.

另请参阅

适用于