WPF 多個選項卡TabControl 頁面分離


此項目源碼下載地址:https://github.com/lizhiqiang0204/TabControl-page-separation

每個頁面的按鍵處理事件直接對應該頁面下的cs文件

 

MainWindow.xaml文件如下

<Window x:Class="WpfApp1.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:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <TabControl>
            <TabItem Header="Page1">
                <Frame Source="/WpfApp1;component/Pages/Page1.xaml"/>
            </TabItem>
            <TabItem Header="Page2">
                <Frame Source="/WpfApp1;component/Pages/Page2.xaml"/>
            </TabItem>
            <TabItem Header="Page3">
                <Frame Source="/WpfApp1;component/Pages/Page3.xaml"/>
            </TabItem>
        </TabControl>
    </Grid>
</Window>

Page1.xaml文件如下:

<Page x:Class="WpfApp1.Pages.Page1"
      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:WpfApp1.Pages"
      mc:Ignorable="d" 
      d:DesignHeight="450" d:DesignWidth="800"
      Title="Page1">

    <Grid>
        <Button Click="Button_Click" Content="Page1" HorizontalAlignment="Left" Margin="213,124,0,0" VerticalAlignment="Top" Width="75" />
    </Grid>
</Page>

Page1.xaml.cs文件如下

namespace WpfApp1.Pages
{
    /// <summary>
    /// Page1.xaml 的交互邏輯
    /// </summary>
    public partial class Page1 : Page
    {
        public Page1()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("頁面1按鍵事件!");
        }
    }
}

其他頁面以此類推,整個運行結果界面如下

如果Page與Window之間有調用關系的話還需要進一步增加點內容,參考https://www.cnblogs.com/lizhiqiang0204/p/11713414.html

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM