WPF仿Tabcontrol實(shí)現(xiàn)切換多個(gè)不同View
在同一塊區(qū)域顯示不同的視圖內(nèi)容,直接使用Tabcontrol,可能要重寫TabItem的控件模板,最直接的方法通過按鈕的切換,控制一個(gè)ContentControl的Content值,實(shí)現(xiàn)切換不同的視圖View。以下是一個(gè)簡單的實(shí)現(xiàn)demo。注:如果用Prism的框架實(shí)現(xiàn),只要設(shè)置Region的區(qū)域塊顯示,會更簡單一些,至少不用自己實(shí)現(xiàn)。
1、ViewA:
<UserControl x:Class="test.viewA" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:test" mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800"> <UserControl.Resources> <SolidColorBrush x:Key="TabControlNormalBorderBrush" Color="#8C8E94"/> <Style x:Key="TabControlStyle1" TargetType="{x:Type TabControl}"> <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/> <Setter Property="Padding" Value="4,4,4,4"/> <Setter Property="BorderThickness" Value="1"/> <Setter Property="BorderBrush" Value="{StaticResource TabControlNormalBorderBrush}"/> <Setter Property="Background" Value="#F9F9F9"/> <Setter Property="HorizontalContentAlignment" Value="Center"/> <Setter Property="VerticalContentAlignment" Value="Center"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type TabControl}"> <Grid ClipToBounds="true" SnapsToDevicePixels="true" KeyboardNavigation.TabNavigation="Local"> <Grid.ColumnDefinitions> <ColumnDefinition x:Name="ColumnDefinition0"/> <ColumnDefinition x:Name="ColumnDefinition1" Width="0"/> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition x:Name="RowDefinition0" Height="Auto"/> <RowDefinition x:Name="RowDefinition1" Height="*"/> </Grid.RowDefinitions> <TabPanel x:Name="HeaderPanel" Grid.Column="0" IsItemsHost="true" Margin="2,2,2,0" Grid.Row="0" KeyboardNavigation.TabIndex="1" Panel.ZIndex="1"/> <Border x:Name="ContentPanel" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Grid.Column="0" KeyboardNavigation.DirectionalNavigation="Contained" Grid.Row="1" KeyboardNavigation.TabIndex="2" KeyboardNavigation.TabNavigation="Local"> <ContentPresenter x:Name="PART_SelectedContentHost" ContentSource="SelectedContent" Margin="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> </Border> </Grid> <ControlTemplate.Triggers> <Trigger Property="TabStripPlacement" Value="Bottom"> <Setter Property="Grid.Row" TargetName="HeaderPanel" Value="1"/> <Setter Property="Grid.Row" TargetName="ContentPanel" Value="0"/> <Setter Property="Height" TargetName="RowDefinition0" Value="*"/> <Setter Property="Height" TargetName="RowDefinition1" Value="Auto"/> <Setter Property="Margin" TargetName="HeaderPanel" Value="2,0,2,2"/> </Trigger> <Trigger Property="TabStripPlacement" Value="Left"> <Setter Property="Grid.Row" TargetName="HeaderPanel" Value="0"/> <Setter Property="Grid.Row" TargetName="ContentPanel" Value="0"/> <Setter Property="Grid.Column" TargetName="HeaderPanel" Value="0"/> <Setter Property="Grid.Column" TargetName="ContentPanel" Value="1"/> <Setter Property="Width" TargetName="ColumnDefinition0" Value="Auto"/> <Setter Property="Width" TargetName="ColumnDefinition1" Value="*"/> <Setter Property="Height" TargetName="RowDefinition0" Value="*"/> <Setter Property="Height" TargetName="RowDefinition1" Value="0"/> <Setter Property="Margin" TargetName="HeaderPanel" Value="2,2,0,2"/> </Trigger> <Trigger Property="TabStripPlacement" Value="Right"> <Setter Property="Grid.Row" TargetName="HeaderPanel" Value="0"/> <Setter Property="Grid.Row" TargetName="ContentPanel" Value="0"/> <Setter Property="Grid.Column" TargetName="HeaderPanel" Value="1"/> <Setter Property="Grid.Column" TargetName="ContentPanel" Value="0"/> <Setter Property="Width" TargetName="ColumnDefinition0" Value="*"/> <Setter Property="Width" TargetName="ColumnDefinition1" Value="Auto"/> <Setter Property="Height" TargetName="RowDefinition0" Value="*"/> <Setter Property="Height" TargetName="RowDefinition1" Value="0"/> <Setter Property="Margin" TargetName="HeaderPanel" Value="0,2,2,2"/> </Trigger> <Trigger Property="IsEnabled" Value="false"> <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> </UserControl.Resources> <UserControl.DataContext> <local:viewAModel/> </UserControl.DataContext> <Grid> <TextBlock Text="{Binding Text}" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="50"/> </Grid> </UserControl>
ViewAViewModel:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; namespace test { public class viewAModel:INotifyPropertyChanged { public viewAModel() { Text = "A"; } public event PropertyChangedEventHandler PropertyChanged; private void Change(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } private string text; public string Text { get { return text; } set { text = value; Change("Text"); } } } }
ViewB:
<UserControl x:Class="test.viewB" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:test" mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800"> <UserControl.DataContext> <local:viewBModel/> </UserControl.DataContext> <Grid> <TextBlock Text="{Binding Text}" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="50"/> </Grid> </UserControl>
ViewBViewModel:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; namespace test { public class viewBModel: INotifyPropertyChanged { public viewBModel() { Text = "ViewB"; } public event PropertyChangedEventHandler PropertyChanged; private void Change(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } private string text; public string Text { get { return text; } set { text = value; Change("Text"); } } } }
在MainWindow切換ViewA和ViewB
<Window x:Class="test.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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:test" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition/> <ColumnDefinition/> </Grid.ColumnDefinitions> <StackPanel> <Button Height="100" Width="100" Content="A" x:Name="A" Click="A_Click"/> <Button Height="100" Width="100" Content="B" x:Name="B" Click="B_Click"/> </StackPanel> <ContentControl x:Name="content" Grid.Column="1"> <local:viewA/> </ContentControl> </Grid> </Window>
MainWindow.cs: 在初始化的時(shí)候,通過一個(gè)Dictionary,把要展示的View先加載出來,存到字典上,確保只Load一次view,在點(diǎn)擊按鈕切換ContentControl時(shí),從字典中獲取,減少每一次都new一個(gè)VIew,造成內(nèi)存的泄露
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace test { /// <summary> /// MainWindow.xaml 的交互邏輯 /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); FramworkElements["A"]=new viewA(); FramworkElements["B"]=new viewB(); } private Dictionary<string, FrameworkElement> FramworkElements = new Dictionary<string, FrameworkElement>(); private void A_Click(object sender, RoutedEventArgs e) { content.Content = FramworkElements["A"]; } private void B_Click(object sender, RoutedEventArgs e) { content.Content = FramworkElements["B"]; } } }
展示的效果:
點(diǎn)擊A按鈕,切換A視圖
點(diǎn)擊B按鈕,切換B視圖:
到此這篇關(guān)于WPF仿Tabcontrol實(shí)現(xiàn)切換多個(gè)不同View的文章就介紹到這了,更多相關(guān)WPF切換不同View內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#對稱加密(AES加密)每次生成的結(jié)果都不同的實(shí)現(xiàn)思路和代碼實(shí)例
這篇文章主要介紹了C#對稱加密(AES加密)每次生成的結(jié)果都不同的實(shí)現(xiàn)思路和代碼實(shí)例,每次解密時(shí)從密文中截取前16位,這就是實(shí)現(xiàn)隨機(jī)的奧秘,本文同時(shí)給出了實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-07-07C#實(shí)現(xiàn)QQ截圖功能及相關(guān)問題
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)QQ截圖功能及相關(guān)問題,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10unity3D實(shí)現(xiàn)三維物體跟隨鼠標(biāo)
這篇文章主要為大家詳細(xì)介紹了unity3D實(shí)現(xiàn)三維物體跟隨鼠標(biāo),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-12-12C#實(shí)現(xiàn)給函數(shù)添加注釋的小技巧分享
這篇文章主要介紹了C#實(shí)現(xiàn)給函數(shù)添加注釋的小技巧,文中通過代碼示例給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-04-04C#調(diào)用百度翻譯實(shí)現(xiàn)翻譯HALCON的示例
HALCON示例程序的描述部分一直是英文的,看起來很不方便。本文就使用百度翻譯實(shí)現(xiàn)翻譯HALCON,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06c# DateTime常用操作實(shí)例(datetime計(jì)算時(shí)間差)
字符串操作DateTime操作,datetime計(jì)算時(shí)間差,取當(dāng)前時(shí)間,更多方法看下面代碼2013-12-12