基于WPF實(shí)現(xiàn)描點(diǎn)導(dǎo)航功能
WPF 實(shí)現(xiàn)描點(diǎn)導(dǎo)航
1.框架支持.NET4 至 .NET8;
2.Visual Studio 2022;
有一位開(kāi)發(fā)者需要實(shí)現(xiàn)類似「左側(cè)導(dǎo)航欄 + 右側(cè)滾動(dòng)內(nèi)容」的控件,需要支持?jǐn)?shù)據(jù)綁定、內(nèi)容模板、同步滾動(dòng)定位等功能。
1. 新增 NavScrollPanel.cs
- 左側(cè)導(dǎo)航欄
ListBox:顯示導(dǎo)航,支持點(diǎn)擊定位; - 右側(cè)滾動(dòng)內(nèi)容區(qū)
ScrollViewer和StackPanel:展示對(duì)應(yīng)內(nèi)容模板,支持滾動(dòng)自動(dòng)選中導(dǎo)航項(xiàng)。 - 通過(guò)
ItemsSource綁定內(nèi)容集合; - 自定義
ItemTemplate顯示內(nèi)容; - 通過(guò)
TranslatePoint方法,可以獲取元素相對(duì)于容器的坐標(biāo)位置,并確保該位置不受Margin的影響。通過(guò)調(diào)用ScrollToVerticalOffset來(lái)滾動(dòng)右側(cè)容器;
public classNavScrollPanel : Control
{
static NavScrollPanel()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(NavScrollPanel),
new FrameworkPropertyMetadata(typeof(NavScrollPanel)));
}
public IEnumerable ItemsSource
{
get => (IEnumerable)GetValue(ItemsSourceProperty);
set => SetValue(ItemsSourceProperty, value);
}
publicstaticreadonly DependencyProperty ItemsSourceProperty =
DependencyProperty.Register(nameof(ItemsSource), typeof(IEnumerable), typeof(NavScrollPanel), new PropertyMetadata(null, OnItemsSourceChanged));
public DataTemplate ItemTemplate
{
get => (DataTemplate)GetValue(ItemTemplateProperty);
set => SetValue(ItemTemplateProperty, value);
}
publicstaticreadonly DependencyProperty ItemTemplateProperty =
DependencyProperty.Register(nameof(ItemTemplate), typeof(DataTemplate), typeof(NavScrollPanel), new PropertyMetadata(null));
publicint SelectedIndex
{
get => (int)GetValue(SelectedIndexProperty);
set => SetValue(SelectedIndexProperty, value);
}
publicstaticreadonly DependencyProperty SelectedIndexProperty =
DependencyProperty.Register(nameof(SelectedIndex), typeof(int), typeof(NavScrollPanel), new PropertyMetadata(-1, OnSelectedIndexChanged));
private ListBox _navListBox;
private ScrollViewer _scrollViewer;
private StackPanel _contentPanel;
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
_navListBox = GetTemplateChild("PART_ListBox") as ListBox;
_scrollViewer = GetTemplateChild("PART_ScrollViewer") as ScrollViewer;
_contentPanel = GetTemplateChild("PART_ContentPanel") as StackPanel;
if (_navListBox != null)
{
_navListBox.DisplayMemberPath = "Title";
_navListBox.SelectionChanged -= NavListBox_SelectionChanged;
_navListBox.SelectionChanged += NavListBox_SelectionChanged;
}
if (_scrollViewer != null)
{
_scrollViewer.ScrollChanged += ScrollViewer_ScrollChanged;
}
RenderContent();
}
private void NavListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
SelectedIndex = _navListBox.SelectedIndex;
}
private void ScrollViewer_ScrollChanged(object sender, ScrollChangedEventArgs e)
{
double currentOffset = _scrollViewer.VerticalOffset;
double viewportHeight = _scrollViewer.ViewportHeight;
for (int i = 0; i < _contentPanel.Children.Count; i++)
{
var element = _contentPanel.Children[i] as FrameworkElement;
if (element == null) continue;
Point relativePoint = element.TranslatePoint(new Point(0, 0), _contentPanel);
if (relativePoint.Y >= currentOffset && relativePoint.Y < currentOffset + viewportHeight)
{
_navListBox.SelectionChanged -= NavListBox_SelectionChanged;
SelectedIndex = i;
_navListBox.SelectionChanged += NavListBox_SelectionChanged;
break;
}
}
}
private static void OnItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is NavScrollPanel control)
{
control.RenderContent();
}
}
private static void OnSelectedIndexChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is NavScrollPanel control)
{
int index = (int)e.NewValue;
if (control._contentPanel != null &&
index >= 0 && index < control._contentPanel.Children.Count)
{
var target = control._contentPanel.Children[index] as FrameworkElement;
if (target != null)
{
var virtualPoint = target.TranslatePoint(new Point(0, 0), control._contentPanel);
control._scrollViewer.ScrollToVerticalOffset(virtualPoint.Y);
}
}
}
}
private void RenderContent()
{
if (_contentPanel == null || ItemsSource == null || ItemTemplate == null)
return;
_contentPanel.Children.Clear();
foreach (var item in ItemsSource)
{
var content = new ContentControl
{
Content = item,
ContentTemplate = ItemTemplate,
Margin = new Thickness(10,50,10,50)
};
_contentPanel.Children.Add(content);
}
}
}
2. 新增 NavScrollPanel.Xaml
控件模板通過(guò) ListBox 和 ScrollViewer 實(shí)現(xiàn)。
<Style TargetType="local:NavScrollPanel">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:NavScrollPanel">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="120" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<ListBox
x:Name="PART_ListBox"
ItemsSource="{TemplateBinding ItemsSource}"
SelectedIndex="{TemplateBinding SelectedIndex}" />
<ScrollViewer
x:Name="PART_ScrollViewer"
Grid.Column="1"
VerticalScrollBarVisibility="Auto">
<StackPanel x:Name="PART_ContentPanel" />
</ScrollViewer>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
3. 使用示例
1. 定義數(shù)據(jù)結(jié)構(gòu)
public class SectionItem {
public string Title { get; set; }
public object Content { get; set; }
}
2. 初始化數(shù)據(jù)并綁定
Sections = new ObservableCollection<SectionItem>
{
new SectionItem{ Title = "播放相關(guān)", Content = new PlaybackSettings()},
new SectionItem{ Title = "桌面歌詞", Content = new DesktopLyrics()},
new SectionItem{ Title = "快捷鍵", Content = new ShortcutKeys()},
new SectionItem{ Title = "隱私設(shè)置", Content = new PrivacySettings()},
new SectionItem{ Title = "關(guān)于我們", Content = new About()},
};
DataContext = this;3. 模板定義
<DataTemplate x:Key="SectionTemplate">
<StackPanel>
<TextBlock Text="{Binding Title}" FontSize="20" Margin="0,10"/>
<Border Background="#F0F0F0" Padding="20" CornerRadius="10">
<ContentPresenter Content="{Binding Content}" FontSize="14"/>
</Border>
</StackPanel>
</DataTemplate>
4. 使用控件
<local:NavScrollPanel
ItemTemplate="{StaticResource SectionTemplate}"
ItemsSource="{Binding Sections}" />5. 新增NavScrollPanelExample.xaml
<wd:Window
x:Class="WpfNavPanel.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfNavPanel"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:wd="https://github.com/WPFDevelopersOrg/WPFDevelopers"
Title="NavScrollPanel - 錨點(diǎn)導(dǎo)航"
Width="800"
Height="450"
mc:Ignorable="d">
<wd:Window.Resources>
<DataTemplate x:Key="SectionTemplate">
<StackPanel>
<TextBlock
Margin="0,10"
FontSize="20"
Text="{Binding Title}" />
<Border
Padding="20"
Background="#F0F0F0"
CornerRadius="10">
<ContentPresenter Content="{Binding Content}" TextElement.FontSize="14" />
</Border>
</StackPanel>
</DataTemplate>
</wd:Window.Resources>
<Grid Margin="4">
<local:NavScrollPanel ItemTemplate="{StaticResource SectionTemplate}" ItemsSource="{Binding Sections}" />
</Grid>
</wd:Window>
效果如下

到此這篇關(guān)于基于WPF實(shí)現(xiàn)描點(diǎn)導(dǎo)航功能的文章就介紹到這了,更多相關(guān)WPF描點(diǎn)導(dǎo)航內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#通過(guò)腳本實(shí)現(xiàn)接口的示例詳解
以前C#腳本用的委托注入模式,今天在提示下,嘗試用腳本直接實(shí)現(xiàn)接口,然后C#可以動(dòng)態(tài)或指定新類型創(chuàng)建接口實(shí)現(xiàn)對(duì)象,下面我們就來(lái)試一下吧2025-05-05
基于C#的UDP協(xié)議的同步通信實(shí)現(xiàn)代碼
本篇文章主要介紹了基于C#的UDP協(xié)議的同步實(shí)現(xiàn)代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02
WinFrom中l(wèi)abel背景透明的實(shí)現(xiàn)方法
這篇文章主要介紹了WinFrom中l(wèi)abel背景透明的實(shí)現(xiàn)方法,方法簡(jiǎn)單實(shí)用,是C#程序設(shè)計(jì)中非常實(shí)用的技巧,需要的朋友可以參考下2014-09-09
關(guān)于C# 4.0新特性“缺省參數(shù)”的實(shí)現(xiàn)詳解
這篇文章主要給大家介紹了關(guān)于C# 4.0新特性“缺省參數(shù)”的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用C# 4.0具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
解決Unity urp級(jí)聯(lián)陰影接縫問(wèn)題
通過(guò)從unity內(nèi)部函數(shù)中抽幾個(gè)出來(lái)改造,強(qiáng)制取某個(gè)裁切球的級(jí)聯(lián)陰影映射,通過(guò)案例給大家詳細(xì)介紹,文中給出了完整的urp shader代碼,對(duì)Unity級(jí)聯(lián)陰影知識(shí)感興趣的朋友一起看看吧2021-06-06
WPF實(shí)現(xiàn)動(dòng)態(tài)加載圖片瀏覽器
這篇文章主要為大家詳細(xì)介紹了一種?WPF?+?Prism?實(shí)現(xiàn)動(dòng)態(tài)分頁(yè)加載圖片的方法,其中結(jié)合了ScrollViewer滾動(dòng)條觸底檢測(cè),實(shí)現(xiàn)邊滾動(dòng)邊加載的流暢體驗(yàn),下面就跟隨小編一起學(xué)習(xí)一下吧2025-04-04

