最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

基于WPF實現(xiàn)面包屑控件的示例代碼

 更新時間:2023年05月17日 10:08:16   作者:WPF開發(fā)者  
這篇文章主要為大家詳細介紹了如何基于WPF實現(xiàn)簡單的面包屑控件,文中的示例代碼講解詳細,對我們學習或工作有一定幫助,感興趣的小伙伴可以了解一下

WPF 實現(xiàn)面包屑控件

  • 框架使用.NET4 至 .NET6
  • Visual Studio 2022
  • 創(chuàng)建 BreadCrumbBar.xaml 設(shè)置 VirtualizingStackPanel 為水平方向顯示 Orientation="Horizontal"
  • 創(chuàng)建 BreadCrumbBarItem.xaml 設(shè)置 ContentPresenter 前顯示符號 > ,通過 BreadCrumbBarConvertr 轉(zhuǎn)換器判斷如果是當前是第 0 個則隱藏顯示符 > 。
  • 創(chuàng)建 BreadCrumbBar.cs 繼承 ListBox 當選中時獲取當前的 SelectedIndex 大于當前的設(shè)置 IsEnabled 設(shè)置 false 反之則為 true

實現(xiàn)代碼

1)創(chuàng)建 BreadCrumbBar.cs 代碼如下:

using?System.Collections;
using?System.Collections.Specialized;
using?System.Windows;
using?System.Windows.Controls;

namespace?WPFDevelopers.Controls
{
????public?class?BreadCrumbBar?:?ListBox
????{
????????static?BreadCrumbBar()
????????{
????????????DefaultStyleKeyProperty.OverrideMetadata(typeof(BreadCrumbBar),?new?FrameworkPropertyMetadata(typeof(BreadCrumbBar)));
????????}
????????public?override?void?OnApplyTemplate()
????????{
????????????base.OnApplyTemplate();
????????}
????????protected?override?void?OnSelectionChanged(SelectionChangedEventArgs?e)
????????{
????????????base.OnSelectionChanged(e);
????????????for?(int?i?=?0;?i?<=?SelectedIndex;?i++)
????????????{
????????????????var?item?=?(ListBoxItem)ItemContainerGenerator.ContainerFromIndex(i);
????????????????if?(item?==?null)?continue;
????????????????if?(!item.IsEnabled)
????????????????????item.IsEnabled?=?true;
????????????}
????????????for?(int?i?=?Items.Count?-?1;?i?>?SelectedIndex;?i--)
????????????{
????????????????var?item?=?(ListBoxItem)ItemContainerGenerator.ContainerFromIndex(i);
????????????????if?(item?==?null)?continue;
????????????????if(item.IsEnabled)
????????????????????item.IsEnabled?=?false;
????????????}
????????}
???????
????????protected?override?bool?IsItemItsOwnContainerOverride(object?item)
????????{
????????????return?item?is?BreadCrumbBarItem;
????????}

????????protected?override?DependencyObject?GetContainerForItemOverride()
????????{
????????????return?new?BreadCrumbBarItem();
????????}
????}
}

2)創(chuàng)建 BreadCrumbBarItem.cs 代碼如下:

using?System;
using?System.Windows;
using?System.Windows.Controls;

namespace?WPFDevelopers.Controls
{
????public?class?BreadCrumbBarItem?:?ListBoxItem
????{
????????private?static?readonly?Type?_typeofSelf?=?typeof(BreadCrumbBarItem);
????????static?BreadCrumbBarItem()
????????{
????????????DefaultStyleKeyProperty.OverrideMetadata(_typeofSelf,
????????????????new?FrameworkPropertyMetadata(_typeofSelf));
????????}
????}
}

3)創(chuàng)建 BreadCrumbBar.xaml 代碼如下:

<ResourceDictionary
????xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
????xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
????xmlns:controls="clr-namespace:WPFDevelopers.Controls"
????xmlns:converts="clr-namespace:WPFDevelopers.Converts">
????<ResourceDictionary.MergedDictionaries>
????????<ResourceDictionary?Source="Basic/ControlBasic.xaml"?/>
????</ResourceDictionary.MergedDictionaries>
????<converts:BreadCrumbBarConvertr?x:Key="WD.BreadCrumbBarConvertr"?/>
????<Style
????????x:Key="WD.BreadCrumbBarItem"
????????BasedOn="{StaticResource?WD.ControlBasicStyle}"
????????TargetType="{x:Type?controls:BreadCrumbBarItem}">
????????<Setter?Property="VerticalContentAlignment"?Value="Center"?/>
????????<Setter?Property="Padding"?Value="6,0"?/>
????????<Setter?Property="FontWeight"?Value="SemiBold"?/>
????????<Setter?Property="FontSize"?Value="{StaticResource?WD.TitleFontSize}"?/>
????????<Setter?Property="Template">
????????????<Setter.Value>
????????????????<ControlTemplate?TargetType="{x:Type?controls:BreadCrumbBarItem}">
????????????????????<StackPanel?Orientation="Horizontal">
????????????????????????<Path
????????????????????????????Name="PART_PathSymbol"
????????????????????????????Width="9"
????????????????????????????Height="9"
????????????????????????????Data="{StaticResource?WD.BreadCrumbBarGeometry}"
????????????????????????????Fill="{DynamicResource?WD.PlaceholderTextSolidColorBrush}"
????????????????????????????IsHitTestVisible="False"
????????????????????????????Stretch="Uniform">
????????????????????????????<Path.Visibility>
????????????????????????????????<MultiBinding?Converter="{StaticResource?WD.BreadCrumbBarConvertr}">
????????????????????????????????????<Binding?RelativeSource="{RelativeSource?AncestorType=ListBoxItem}"?/>
????????????????????????????????????<Binding?Path="SelectedIndex"?RelativeSource="{RelativeSource?AncestorType=ListBox}"?/>
????????????????????????????????</MultiBinding>
????????????????????????????</Path.Visibility>
????????????????????????</Path>
????????????????????????<ContentPresenter
????????????????????????????x:Name="PART_ContentPresenter"
????????????????????????????Margin="{TemplateBinding?Padding}"
????????????????????????????HorizontalAlignment="{TemplateBinding?HorizontalContentAlignment}"
????????????????????????????VerticalAlignment="{TemplateBinding?VerticalContentAlignment}"
????????????????????????????SnapsToDevicePixels="{TemplateBinding?SnapsToDevicePixels}"
????????????????????????????TextElement.Foreground="{TemplateBinding?Foreground}"?/>
????????????????????</StackPanel>
????????????????????<ControlTemplate.Triggers>
????????????????????????<Trigger?Property="IsMouseOver"?Value="True">
????????????????????????????<Setter?Property="Foreground"?Value="{DynamicResource?WD.PrimaryMouseOverSolidColorBrush}"?/>
????????????????????????</Trigger>
????????????????????</ControlTemplate.Triggers>
????????????????</ControlTemplate>
????????????</Setter.Value>
????????</Setter>
????</Style>
????<Style
????????x:Key="WD.BreadCrumbBar"
????????BasedOn="{StaticResource?WD.ControlBasicStyle}"
????????TargetType="{x:Type?controls:BreadCrumbBar}">
????????<Setter?Property="Height"?Value="40"?/>
????????<Setter?Property="Margin"?Value="5"?/>
????????<Setter?Property="ItemsPanel">
????????????<Setter.Value>
????????????????<ItemsPanelTemplate>
????????????????????<VirtualizingStackPanel?Orientation="Horizontal"?/>
????????????????</ItemsPanelTemplate>
????????????</Setter.Value>
????????</Setter>
????????<Setter?Property="ItemContainerStyle"?Value="{StaticResource?WD.BreadCrumbBarItem}"?/>
????????<Setter?Property="Template">
????????????<Setter.Value>
????????????????<ControlTemplate?TargetType="{x:Type?controls:BreadCrumbBar}">
????????????????????<Border
????????????????????????Margin="{TemplateBinding?Margin}"
????????????????????????Background="{TemplateBinding?Background}"
????????????????????????BorderBrush="{TemplateBinding?BorderBrush}"
????????????????????????BorderThickness="{TemplateBinding?BorderThickness}">
????????????????????????<ScrollViewer?Grid.Column="1"?HorizontalScrollBarVisibility="Auto">
????????????????????????????<ItemsPresenter?x:Name="ItemsHost"?/>
????????????????????????</ScrollViewer>
????????????????????</Border>
????????????????</ControlTemplate>
????????????</Setter.Value>
????????</Setter>
????</Style>
????<Style?BasedOn="{StaticResource?WD.BreadCrumbBar}"?TargetType="{x:Type?controls:BreadCrumbBar}"?/>
</ResourceDictionary>

4)創(chuàng)建 BreadCrumbBarConverter.cs 代碼如下:

using?System;
using?System.Globalization;
using?System.Windows;
using?System.Windows.Controls;
using?System.Windows.Data;

namespace?WPFDevelopers.Converts
{
????public?class?BreadCrumbBarConvertr?:?IMultiValueConverter
????{
????????public?object?Convert(object[]?values,?Type?targetType,?object?parameter,?CultureInfo?culture)
????????{
????????????var?item?=?values[0]?as?ListBoxItem;
????????????var?listBox?=?ItemsControl.ItemsControlFromItemContainer(item)?as?ListBox;
????????????if?(listBox?==?null)?return?Visibility.Collapsed;
????????????var?arrayIndex?=?listBox.ItemContainerGenerator.IndexFromContainer(item);
????????????if?(arrayIndex?==?0)
????????????????return?Visibility.Collapsed;
????????????return?Visibility.Visible;
????????}

????????public?object[]?ConvertBack(object?value,?Type[]?targetTypes,?object?parameter,?CultureInfo?culture)
????????{
????????????throw?new?NotImplementedException();
????????}
????}
}

5)創(chuàng)建 BreadCrumbBarExample.xaml 代碼如下:

<Grid>
????????????<Grid.RowDefinitions>
????????????????<RowDefinition?Height="Auto"?/>
????????????????<RowDefinition?/>
????????????????<RowDefinition?Height="Auto"?/>
????????????</Grid.RowDefinitions>
????????????<wd:BreadCrumbBar
????????????????DisplayMemberPath="Text"
????????????????ItemsSource="{Binding?BreadcrumbItems,?RelativeSource={RelativeSource?AncestorType=UserControl}}"
????????????????SelectedItem="{Binding?BreadcrumbItem,?RelativeSource={RelativeSource?AncestorType=UserControl}}"
????????????????SelectionChanged="BreadCrumbBar_SelectionChanged"?/>
????????????<Frame
????????????????Name="myFrame"
????????????????Grid.Row="1"
????????????????NavigationUIVisibility="Hidden"?/>
????????????<Button
????????????????Grid.Row="2"
????????????????Width="80"
????????????????Click="btnNext_Click"
????????????????Content="Next"
????????????????Style="{StaticResource?WD.SuccessPrimaryButton}"?/>
????????</Grid>

6) BreadCrumbBarExample.xaml.cs 代碼如下:

using?System.Collections.Generic;
using?System;
using?System.Windows.Controls;
using?System.Collections.ObjectModel;
using?System.Windows;
using?System.Reflection;

namespace?WPFDevelopers.Samples.ExampleViews
{
????///?<summary>
????///?StepExample.xaml?的交互邏輯
????///?</summary>
????public?partial?class?BreadCrumbBarExample?:?UserControl
????{
????????private?int?index?=?0;
????????private?List<BreadcrumbItem>?Breadcrumbs?=?new?List<BreadcrumbItem>();

????????public?ObservableCollection<BreadcrumbItem>?BreadcrumbItems
????????{
????????????get?{?return?(ObservableCollection<BreadcrumbItem>)GetValue(BreadcrumbItemsProperty);?}
????????????set?{?SetValue(BreadcrumbItemsProperty,?value);?}
????????}

????????public?static?readonly?DependencyProperty?BreadcrumbItemsProperty?=
????????????DependencyProperty.Register("BreadcrumbItems",?typeof(ObservableCollection<BreadcrumbItem>),?typeof(BreadCrumbBarExample),?new?PropertyMetadata(null));

????????public?BreadcrumbItem?BreadcrumbItem
????????{
????????????get?{?return?(BreadcrumbItem)GetValue(BreadcrumbItemProperty);?}
????????????set?{?SetValue(BreadcrumbItemProperty,?value);?}
????????}

????????public?static?readonly?DependencyProperty?BreadcrumbItemProperty?=
????????????DependencyProperty.Register("BreadcrumbItem",?typeof(BreadcrumbItem),?typeof(BreadCrumbBarExample),?new?PropertyMetadata(null));

????????public?BreadCrumbBarExample()
????????{
????????????InitializeComponent();
????????????Loaded?+=?BreadCrumbBarExample_Loaded;

????????}

????????private?void?BreadCrumbBarExample_Loaded(object?sender,?RoutedEventArgs?e)
????????{
????????????var?breadcrumbItems?=?new?List<BreadcrumbItem>()
????????????{
????????????????new?BreadcrumbItem()?{?Text?=?"主頁"?,?Uri=new?Uri("pack://application:,,,/WPFDevelopers.Samples;component/ExampleViews/DrawerMenu/HomePage.xaml",UriKind.Absolute?)?},
????????????????new?BreadcrumbItem()?{?Text?=?"Edge",?Uri=new?Uri("pack://application:,,,/WPFDevelopers.Samples;component/ExampleViews/DrawerMenu/EdgePage.xaml"?,UriKind.Absolute?)?},
????????????????new?BreadcrumbItem()?{?Text?=?"郵件",?Uri=new?Uri("pack://application:,,,/WPFDevelopers.Samples;component/ExampleViews/DrawerMenu/EmailPage.xaml"?,UriKind.Absolute?)?},

????????????};
????????????Breadcrumbs?=?breadcrumbItems;
????????????myFrame.Navigate(Breadcrumbs[index].Uri);
????????????BreadcrumbItems?=?new?ObservableCollection<BreadcrumbItem>()?{?Breadcrumbs[index]?};
????????}
????????private?void?BreadCrumbBar_SelectionChanged(object?sender,?SelectionChangedEventArgs?e)
????????{
????????????myFrame.Navigate(BreadcrumbItem.Uri);
????????????index?=?BreadcrumbItems.IndexOf(BreadcrumbItem);
????????}

????????private?void?btnNext_Click(object?sender,?RoutedEventArgs?e)
????????{
????????????index?+=?1;
????????????if?(index?>=?Breadcrumbs.Count)?return;
????????????var?model?=?Breadcrumbs[index];
????????????if?(BreadcrumbItems.Contains(model))
????????????{
????????????????BreadcrumbItem?=?model;
????????????????return;
????????????}
????????????BreadcrumbItems.Add(model);
????????????BreadcrumbItem?=?model;
????????}

????}
????public?class?BreadcrumbItem
????{
????????public?string?Text?{?get;?set;?}
????????public?Uri?Uri?{?get;?set;?}
????}
}

效果圖

到此這篇關(guān)于基于WPF實現(xiàn)面包屑控件的示例代碼的文章就介紹到這了,更多相關(guān)WPF面包屑控件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 解析abstract與override究竟可不可以同時使用

    解析abstract與override究竟可不可以同時使用

    本篇文章是對abstract與override究竟可不可以同時使用進行了詳細分析介紹,需要的朋友參考下
    2013-05-05
  • C#中委托的+=和-=深入研究

    C#中委托的+=和-=深入研究

    這篇文章主要介紹了C#中委托的+=和-=深入研究,本文深入研究+=和-=在執(zhí)行時都做了哪些事情,加深對C#委托的理解和使用,需要的朋友可以參考下
    2015-01-01
  • C#根據(jù)權(quán)重抽取隨機數(shù)

    C#根據(jù)權(quán)重抽取隨機數(shù)

    最近在開發(fā)過程中遇到一個需要做帶權(quán)隨機的處理,本文主要介紹了C#根據(jù)權(quán)重抽取隨機數(shù),具有一定的參考價值,感興趣的可以了解一下
    2024-02-02
  • 基于C#生成隨機數(shù)示例

    基于C#生成隨機數(shù)示例

    這篇文章主要介紹了基于C#生成隨機數(shù)的類文件,可作為公共庫供其他文件調(diào)用,需要的朋友可以參考下
    2014-07-07
  • C#獲取DataTable對象狀態(tài)DataRowState

    C#獲取DataTable對象狀態(tài)DataRowState

    這篇文章介紹了C#獲取DataTable對象狀態(tài)DataRowState的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-02-02
  • C#使用Aspose.Cells控件讀取Excel

    C#使用Aspose.Cells控件讀取Excel

    本文介紹Aspose.Cells基礎(chǔ)的用法,供大家參考。
    2016-03-03
  • 使用C#與設(shè)備接口進行無縫通信的實現(xiàn)技巧

    使用C#與設(shè)備接口進行無縫通信的實現(xiàn)技巧

    隨著物聯(lián)網(wǎng)、智能設(shè)備和自動化技術(shù)的快速發(fā)展,越來越多的設(shè)備需要與計算機系統(tǒng)進行實時通信,而C#作為一門強大的編程語言,憑借其廣泛的庫支持和高效的開發(fā)效率,已成為與設(shè)備接口對接的理想選擇,在本篇文章中,我們將探討如何使用C#與設(shè)備進行無縫通信
    2025-01-01
  • 舉例講解C#編程中委托的實例化使用

    舉例講解C#編程中委托的實例化使用

    這篇文章主要介紹了C#編程中委托的實例化使用,包括委托的聲明和多播委托的創(chuàng)建等內(nèi)容,需要的朋友可以參考下
    2016-01-01
  • ASP.NET C#中Application的用法教程

    ASP.NET C#中Application的用法教程

    這篇文章主要給大家介紹了關(guān)于ASP.NET C#中Application的用法,在介紹Application的用法之前,先給大家介紹了Session的用法供大家參考學習,文中介紹的非常詳細,需要的朋友們下面跟著小編一起來學習學習吧。
    2017-05-05
  • C?sharp?(#)?數(shù)據(jù)類型獲取方式

    C?sharp?(#)?數(shù)據(jù)類型獲取方式

    這篇文章主要介紹了C?sharp?(#)?數(shù)據(jù)類型獲取方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-11-11

最新評論

阜新市| 台江县| 奉新县| 肇庆市| 南召县| 壶关县| 民权县| 安多县| 沛县| 和静县| 连城县| 沙洋县| 敦煌市| 莱阳市| 肇庆市| 东方市| 昭觉县| 黎城县| 洛宁县| 林州市| 永安市| 青冈县| 万载县| 牙克石市| 婺源县| 罗田县| 洪洞县| 祁连县| 万全县| 饶阳县| 玉屏| 衡水市| 广南县| 开平市| 彭泽县| 彭水| 南平市| 湟中县| 靖安县| 北海市| 和龙市|