我對 WPF .net 5.0 框架非常陌生。每次我導航到框架中的不同頁面時,它都會保持滾動位置,而不是回傳頁面頂部。我可以知道是否有辦法確保每次導航時它都會進入頁面頂部?謝謝!
這是xml
<ScrollViewer Height="1000" Width="1000" HorizontalScrollBarVisibility="Auto">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="23*"/>
<RowDefinition Height="477*"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<TextBlock HorizontalAlignment="Left" Margin="10,0,0,0" TextWrapping="Wrap" VerticalAlignment="Center" FontWeight="Bold" Foreground="#FF5B5858"><Run Language="en-sg" Text="ADD NEW APPLICATION"/></TextBlock>
</Grid>
<Grid Grid.Row="1" Margin="0,10,0,0">
<Border CornerRadius="0,0,30,0" BorderThickness="1">
<Frame x:Name="wizardFrame" Content="Frame" HorizontalAlignment="Center" Width="1002" Margin="0,9,0,471" NavigationUIVisibility="Hidden"/>
</Border>
</Grid>
</Grid>
</ScrollViewer>
這是后面的代碼
wizardFrame.NavigationService.Navigate(new LocationPage1());
這就是我用來導航到框架中不同頁面的方法
this.NavigationService.Navigate(new GenerateApplicationNumberPage2());
uj5u.com熱心網友回復:
您的問題是 ScrollViewer 與 Frame 無關,因此它不會對 Frame 導航做出反應。
為了解決這個問題,我看到了兩種解決方案(選擇一個):
- 您命名您的 ScrollViewer(例如:mainScroll),并在代碼隱藏中訂閱
NavigatedFrame 的事件,以便按照ScrollViewer.ScrollToTop()評論中的建議呼叫 。
// Somewhere in the constructor after InitializeComponent();
wizardFrame.Navigated = (_, __) => mainScroll.ScrollToTop();
- 您從主頁面中洗掉 ScrollViewer,并在每個頁面(、、和任何其他頁面)的 XAML 中添加一個作為根
LocationPage1元素GenerateApplicationNumberPage2。它會起作用,因為您每次導航時都會創建一個新頁面(這對資源管理不是很好,但這不是這里的主題)。
不同之處在于TextBlock“添加新應用程式”將始終在頁面上方可見,而不是在 ScrollViewer 內。
<Page x:Class="Namespace.LocationPage1">
<ScrollViewer Height="1000" Width="1000">
<!-- Your page content here -->
</ScrollViewer>
</Page>
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/472183.html
上一篇:WPF繪圖輪顏色選擇器?
