WPF 复制/克隆 TabItem 包括属性和结构

WPF 复制/克隆 TabItem 包括属性和结构


    /// 使用方法如下
    /// 新建一个 TabItem 对象
    /// System.Windows.Controls.TabItem NewTabItem = new TabItem();
    /// 复制一个已存在的 TabItem 的属性到另一个 TabItem
    /// NewTabItem.CopyPropertiesFrom(TabItem1);
    /// 添加一个 TabItem 到用户界面
    /// tabControl.Items.Add(NewTabItem);

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows;

namespace BLL
{
    /// <summary>
    /// 扩展方法 - 从指定对象复制属性
    /// </summary>
    public static class CopyTabItem
    {
        /// <summary>
        /// 从指定对象复制属性
        /// </summary>
        /// <param name="controlToSet"></param>
        /// <param name="controlToCopy"></param>
        public static void CopyPropertiesFrom(this FrameworkElement controlToSet, FrameworkElement controlToCopy)
        {
            foreach (var dependencyValue in GetAllProperties(controlToCopy).Where((item) => !item.ReadOnly).ToDictionary(dependencyProperty => dependencyProperty, controlToCopy.GetValue))
            {
                controlToSet.SetValue(dependencyValue.Key, dependencyValue.Value);
            }
        }

        /// <summary>
        /// 得到所有属性
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static IList<DependencyProperty> GetAllProperties(DependencyObject obj)
        {
            return (from PropertyDescriptor pd in TypeDescriptor.GetProperties(obj, new Attribute[]
            {
                new PropertyFilterAttribute(PropertyFilterOptions.SetValues)
            })
                    select DependencyPropertyDescriptor.FromProperty(pd)
                    into dpd
                    where dpd != null
                    select dpd.DependencyProperty).ToList();
        }
    }
}


以上代码大部分来源于: CodeGo.net  >  复制的TabItem与MVVM结构 http://codego.net/606080/
WPF中,`TabControl`控件用于组织多个`TabItem`,每个`TabItem`代表一个独立的内容区域。如果你想要实现关闭打开`TabItem`的功能,你可以通过以下步骤创建一个简单的示例: 首先,在XAML中设置`TabControl`的基本结构: ```xml <Window x:Class="YourNamespace.TabControlExample" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="TabControl Example" Height="450" Width="800"> <TabControl> <TabItem Header="Tab 1"> <StackPanel> <!-- Tab 1 Content goes here --> </StackPanel> </TabItem> <TabItem Header="Tab 2"> <StackPanel> <!-- Tab 2 Content goes here --> </StackPanel> </TabItem> </TabControl> </Window> ``` 然后,在后台(例如`MainWindow.xaml.cs`)中添加事件处理以实现关闭打开`TabItem`: ```csharp using System.Windows; using System.Windows.Controls; public partial class TabControlExample : Window { public TabControlExample() { InitializeComponent(); // Add event handlers to the TabItems foreach (var tabItem in this.tabControl.ItemsSource) { tabItem.PreviewClosing += OnTabPreviewClosing; tabItem.RequestBringIntoView += OnTabRequestBringIntoView; } } private void OnTabPreviewClosing(object sender, CancelEventArgs e) { if (e.Cancel) return; // Remove the closed TabItem from the collection var item = (TabItem)sender; ((ICollectionView)this.tabControl.ItemsSource).Remove(item); } private void OnTabRequestBringIntoView(object sender, EventArgs e) { // Bring the tab back into view after closing and reopening var tabItem = (TabItem)sender; tabItem.IsSelected = true; } } ``` 在这个例子中,我们设置了`PreviewClosing`事件监听,当用户尝试关闭`TabItem`时,会触发`OnTabPreviewClosing`方法。如果关闭操作被取消(默认情况下),我们就从数据集`ICollectionView`中移除它。 同时,我们也设置了`RequestBringIntoView`事件,当`TabItem`被关闭再重新打开时,它会被自动选中并显示在视图上。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lovevalesail

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值