其他分享
首页 > 其他分享> > WPF基础五:UI③带标题内容控件TabControl 

WPF基础五:UI③带标题内容控件TabControl 

作者:互联网

TabControl

表示包含多个项的控件,这些项共享屏幕上的同一空间。

TabControl有助于最大程度地减少屏幕空间使用量,同时允许应用程序公开大量数据。 TabControl包含共享同一屏幕空间的多个 TabItem 对象。一次只能看到 TabControl 中的一个  TabItem 。 当用户选择的 TabItem 选项卡时,将显示 TabItem 的内容,并且隐藏其他 TabItem 对象的内容。

TabControl 为 ItemsControl ,这意味着它可以包含任何类型的对象的集合 (例如字符串、图像或面板) 。 有关更多信息,请参见 ItemsControl 类。

名称备注权限

ContentStringFormatProperty

标识 ContentStringFormat 依赖项属性。public static readonly

ContentTemplateProperty

标识 ContentTemplate 依赖项属性。public static readonly

ContentTemplateSelectorProperty

标识 ContentTemplateSelector 依赖项属性。public static readonly

SelectedContentProperty

标识 SelectedContent 依赖项属性。public static readonly

SelectedContentStringFormatProperty

标识 SelectedContentStringFormat 依赖项属性。public static readonly

SelectedContentTemplateProperty

标识 SelectedContentTemplate 依赖项属性。public static readonly

SelectedContentTemplateSelectorProperty

标识 SelectedContentTemplateSelector 依赖项属性。public static readonly

TabStripPlacementProperty

标识 TabStripPlacement 依赖项属性。public static readonly
名称备注权限

ContentStringFormat

获取一个撰写字符串,该字符串指定如果 TabItem 对象的内容显示为字符串,应如何设置这些内容的格式。get;

ContentTemplate

获取或设置 DataTemplate 以应用于所有未定义 TabItem 或 ContentTemplate 属性的 ContentTemplateSelectorget; set;

ContentTemplateSelector

获取或设置为选择用于显示控件内容的模板提供自定义逻辑的 DataTemplateSelectorget; set;

SelectedContent

获取当前选择的 TabItem 的内容。get;

SelectedContentStringFormat

获取一个撰写字符串,该字符串指定如果当前选定的 TabItem 的内容显示为字符串,应如何设置该内容的格式。get;

SelectedContentTemplate

获取当前所选项的 DataTemplateget;

SelectedContentTemplateSelector

获取当前所选项的 DataTemplateSelectorget;

TabStripPlacement

获取或设置选项卡标题如何相对于选项卡内容进行对齐。get; set;
名称备注权限

GetContainerForItemOverride

创建或标识用于显示指定项的元素。protected

IsItemItsOwnContainerOverride

确定指定项是否是(或可作为)其自己的 ItemContainerprotected

OnApplyTemplate

调用 ApplyTemplate() 时进行调用。protected

OnCreateAutomationPeer

提供 TabControlAutomationPeer 此控件的适当实现,作为 WPF 自动化基础结构的一部分。protected

OnItemsChanged

当 IsInitialized 设置为 true 时调用。protected

OnItemsChanged

调用以在项更改时更新当前选择。protected

OnKeyDown

为用户按任意键时引发的 KeyDown 路由事件提供类处理。protected

OnSelectionChanged

引发 SelectionChanged 路由事件。protected

TabItem

表示 TabControl 内某个可选择的项。

TabItem 为 HeaderedContentControl。 它的内容属性为 Content ,并且其标头属性为 Header 。 有关更多信息,请参见 HeaderedContentControl 类。

自定义 TabControl 控件

若要对多个TabItem控件应用相同的属性设置  ,请使用 Style 属性。 您可以修改ControlTemplate 默认值 ,为控件指定独特的外观。 有关创建ControlTemplate的详细信息  ,请参阅 通过创建 System.windows.controls.controltemplate> 自定义现有控件的外观。 若要查看特定于 TabItem 的部分和状态,请参阅 TabControl 样式和模板

此控件的依赖属性可能由控件的默认样式设置。 如果按默认样式设置属性,则当控件出现在应用程序中时,属性可能会更改为默认值。 默认样式取决于应用程序运行时使用的桌面主题。 有关详细信息,请参阅 默认的 WPF 主题

只有视觉对象属性已存在于控件的默认模板中并且已使用 TemplateBinding 设置时,设置该属性才有效。 在通过创建 ControlTemplate 自定义现有控件的外观一文的更改控件的视觉结构部分可以找到视觉属性列表。

名称备注权限

IsSelectedProperty

标识 IsSelected 依赖项属性。public static readonly

TabStripPlacementProperty

标识 TabStripPlacement 依赖项属性。public static readonly
名称备注权限

IsSelected

获取或设置一个值,该值指示是否选择 TabItemget; set;

TabStripPlacement

获取选项卡区域位置。get;
名称备注权限

OnAccessKey

在调用 AccessKey 的 TabControl 时响应。protected

OnContentChanged

当 Content 属性更改时调用。protected

OnContentTemplateChanged

当 ContentTemplate 属性更改时调用。protected

OnContentTemplateSelectorChanged

当 ContentTemplateSelector 属性更改时调用。protected

OnCreateAutomationPeer

提供 TabItemAutomationPeer 此控件的适当实现,作为 WPF 自动化基础结构的一部分。protected

OnMouseLeftButtonDown

响应 MouseLeftButtonDown 事件。protected

OnPreviewGotKeyboardFocus

宣布键盘焦点位于此元素上。protected

OnSelected

调用以指示 IsSelected 属性已更改为 trueprotected

OnUnselected

调用以指示 IsSelected 属性已更改为 falseprotected

TabControl有一个比较重要的属性TabStripPlacement,它决定选项卡的位置。是一个Dock属性值,指示选项卡停靠在控件的上、下、左、右。默认值为 Top

TabControl派生自Selector

Selector有几个比较重要的属性:

Selector的事件:

TabItem是TabControl中的项。

 

TabItem派生自HeaderedContentControl:


XAML范例

<Window
    x:Class="TabControlDemo.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:TabControlDemo"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Title="MainWindow"
    Width="500"
    Height="300"
    mc:Ignorable="d">
    <TabControl SelectedIndex="1" SelectionChanged="OnSelectionChanged" TabStripPlacement="Right">
        <TabItem Header="TabItem1">
            <StackPanel>
                <Label />
                <Button Content="TabItem2 IsSelected" Click="OnTabItem2"/>
            </StackPanel>
        </TabItem>
        <TabItem Header="TabItem2">
            <StackPanel>
                <Label />
                <Button Content="TabItem1 IsSelected"  Click="OnTabItem1"/>
            </StackPanel>
        </TabItem>
    </TabControl>
</Window>

 

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 TabControlDemo
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            foreach (var item in (((sender as TabControl).SelectedItem as TabItem).Content as StackPanel).Children)
            {
                if(item is Label) (item as Label).Content = "SelectedIndex: " + (sender as TabControl).SelectedIndex.ToString();
            }          
        }

        private void OnTabItem2(object sender, RoutedEventArgs e)
        {
            List<TabItem> tabItems = new List<TabItem>();
            foreach (var item in ((((sender as Button).Parent as StackPanel).Parent as TabItem).Parent as TabControl).Items)
            {
                if (item is TabItem) tabItems.Add(item as TabItem);
            }
            tabItems[1].IsSelected = true;
        }
          

        private void OnTabItem1(object sender, RoutedEventArgs e)
        {
            List<TabItem> tabItems = new List<TabItem>();
            foreach (var item in ((((sender as Button).Parent as StackPanel).Parent as TabItem).Parent as TabControl).Items)
            {
                if (item is TabItem) tabItems.Add(item as TabItem);
            }
            tabItems[0].IsSelected = true;
        }
    }
}

C#范例

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 TabControlDemo
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            TabControl tabControl = new TabControl();
            tabControl.SelectedItem = 1;
            tabControl.SelectionChanged += OnSelectionChanged;
            tabControl.TabStripPlacement = Dock.Right;

            TabItem tabItem1 = new TabItem();
            TabItem tabItem2 = new TabItem();
            StackPanel stackPanel1 = new StackPanel();
            StackPanel stackPanel2 = new StackPanel();
            Label label1 = new Label();
            Label label2 = new Label();
            Button button1 = new Button();
            Button button2 = new Button();

            button1.Content = "TabItem2 IsSelected";
            button1.Click += OnTabItem2;
            button2.Content = "TabItem1 IsSelected";
            button2.Click += OnTabItem1;

            stackPanel1.Children.Add(label1);
            stackPanel1.Children.Add(button1);
            stackPanel2.Children.Add(label2);
            stackPanel2.Children.Add(button2);
            tabItem1.Header = "TabItem1";
            tabItem1.Content = stackPanel1;
            tabItem2.Header = "TabItem2";
            tabItem2.Content = stackPanel2;
            tabControl.Items.Add(tabItem1);
            tabControl.Items.Add(tabItem2);

            this.Content = tabControl;
        }

        private void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            foreach (var item in (((sender as TabControl).SelectedItem as TabItem).Content as StackPanel).Children)
            {
                if(item is Label) (item as Label).Content = "SelectedIndex: " + (sender as TabControl).SelectedIndex.ToString();
            }          
        }

        private void OnTabItem2(object sender, RoutedEventArgs e)
        {
            List<TabItem> tabItems = new List<TabItem>();
            foreach (var item in ((((sender as Button).Parent as StackPanel).Parent as TabItem).Parent as TabControl).Items)
            {
                if (item is TabItem) tabItems.Add(item as TabItem);
            }
            tabItems[1].IsSelected = true;
        }
          

        private void OnTabItem1(object sender, RoutedEventArgs e)
        {
            List<TabItem> tabItems = new List<TabItem>();
            foreach (var item in ((((sender as Button).Parent as StackPanel).Parent as TabItem).Parent as TabControl).Items)
            {
                if (item is TabItem) tabItems.Add(item as TabItem);
            }
            tabItems[0].IsSelected = true;
        }
    }
}

 


 

标签:控件,System,UI,using,WPF,属性,TabControl,TabItem
来源: https://blog.csdn.net/hd51cc/article/details/116088686