0

我有一个有效的 Prism 应用程序。我可以从 CheckBox 添加和关闭 TabControl 中的 TabItem。当 TabItem 关闭或添加时,我无法取消选中复选框。请有人帮帮我。

我正在使用棱镜区域适配器和区域行为,如下所示。

对接管理器区域适配器

using Syncfusion.Windows.Tools.Controls;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace Common.Helpers.Regions
{
    public class DockingManagerRegionAdapter : RegionAdapterBase<DockingManager>
    {
        /// <summary>
        /// Used to determine what views were injected and ContentPanes were generated for
        /// </summary>
        private static readonly DependencyProperty IsGeneratedProperty = DependencyProperty.RegisterAttached("IsGenerated", typeof(bool), typeof(DockingManagerRegionAdapter), null);
        /// <summary>
        /// Used to track the region that a ContentPane belongs to so that we can access the region from within the ContentPane.Closed event handler
        /// </summary>
        private static readonly DependencyProperty RegionProperty = DependencyProperty.RegisterAttached("Region", typeof(IRegion), typeof(DockingManagerRegionAdapter), null);


        public DockingManagerRegionAdapter(IRegionBehaviorFactory regionBehaviorFactory)
            : base(regionBehaviorFactory)
        {

        }
        protected override void Adapt(IRegion region, DockingManager regionTarget)
        {
            if (regionTarget.ItemsSource != null)
                throw new InvalidOperationException("ItemsSource property is not empty.This control is being associated with a region, but the control is already bound to something else.If you did not explicitly set the control’s ItemSource property, this exception may be caused by a change in the value of the inherited RegionManager attached property.");


            if (region == null) throw new ArgumentNullException(nameof(region));
            if (regionTarget == null) throw new ArgumentNullException(nameof(regionTarget));

            region.Views.CollectionChanged += (s, e) =>
            {
                if (e.Action == NotifyCollectionChangedAction.Add)
                {
                    foreach (FrameworkElement view in e.NewItems)
                    {
                        AddViewToRegion(view, regionTarget);
                    }
                }
                else if (e.Action == NotifyCollectionChangedAction.Remove)
                {
                    foreach (FrameworkElement view in e.OldItems)
                    {
                        RemoveViewFromRegion(view, regionTarget);
                    }
                }
            };
            regionTarget.WindowClosing += delegate (object sender, WindowClosingEventArgs args)
            {
                var child = args.TargetItem as UserControl;
                region.Remove(child);
            };
            regionTarget.CloseButtonClick += delegate (object sender, CloseButtonEventArgs args)
            {
                var child = args.TargetItem as UserControl;
                region.Remove(child);
            };
        }

        protected override IRegion CreateRegion()
        {
            return new SingleActiveRegion();
        }
        private static void AddViewToRegion(object view, DockingManager dockingManager)
        {
            if (view is ContentControl document)
            {
                if (!dockingManager.Children.Contains(document))
                {
                    dockingManager.BeginInit();
                    dockingManager.Children.Add(document);
                    dockingManager.EndInit();
                    dockingManager.ActiveWindow = document;
                }
            }
        }
        private static void RemoveViewFromRegion(object view, DockingManager dockingManager)
        {
            if (view is ContentControl document)
            {
                dockingManager.BeginInit();
                dockingManager.Children.Remove(document);
                dockingManager.EndInit();
               
            }
        }
        protected override void AttachBehaviors(IRegion region, DockingManager regionTarget)
        {
            base.AttachBehaviors(region, regionTarget);

            if (!region.Behaviors.ContainsKey(DocumentRegionActiveAwareBehavior.BehaviorKey))
                region.Behaviors.Add(DocumentRegionActiveAwareBehavior.BehaviorKey, new DocumentRegionActiveAwareBehavior { HostControl = regionTarget });
        }
        
    }
}

DocumentRegionActiveAwareBehavior

using Prism.Regions.Behaviors;
using Syncfusion.Windows.Tools.Controls;
using System.Collections.Specialized;
using System.Windows;
using System.Windows.Controls;

namespace Common.Helpers.Regions
{
    public class DocumentRegionActiveAwareBehavior : RegionBehavior, IHostAwareRegionBehavior
    {
        public const string BehaviorKey = "DocumentRegionActiveAwareBehavior";
        DependencyObject _hostControl;
        public DependencyObject HostControl
        {
            get { return _hostControl; }
            set { _hostControl = value as DockingManager; }
        }

        protected override void OnAttach()
        {
            ((HostControl as DockingManager).DocContainer as DocumentContainer).AddTabDocumentAtLast = true;
            (HostControl as DockingManager).ActiveWindowChanged += DocumentRegionActiveAwareBehavior_ActiveWindowChanged;
            Region.ActiveViews.CollectionChanged += ActiveViews_CollectionChanged;
        }

        private void DocumentRegionActiveAwareBehavior_ActiveWindowChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (e.OldValue != null)
            {
                var item = e.OldValue;

                //are we dealing with a ContentPane directly
                if (Region.Views.Contains(item) && Region.ActiveViews.Contains(item))
                {
                    Region.Deactivate(item);
                }
                else
                {
                    //now check to see if we have any views that were injected
                    var contentControl = item as ContentControl;
                    if (contentControl != null)
                    {
                        var injectedView = contentControl.Content;
                        if (Region.Views.Contains(injectedView) && Region.ActiveViews.Contains(injectedView))
                            Region.Deactivate(injectedView);
                    }
                }
            }

            if (e.NewValue != null)
            {
                var item = e.NewValue;

                //are we dealing with a ContentPane directly
                if (Region.Views.Contains(item) && !this.Region.ActiveViews.Contains(item))
                {
                    Region.Activate(item);
                }
                else
                {
                    //now check to see if we have any views that were injected
                    var contentControl = item as ContentControl;
                    if (contentControl != null)
                    {
                        var injectedView = contentControl.Content;
                        if (Region.Views.Contains(injectedView) && !this.Region.ActiveViews.Contains(injectedView))
                            Region.Activate(injectedView);
                    }
                }
            }
        }

        void ActiveViews_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.Action == NotifyCollectionChangedAction.Add)
            {
                //are we dealing with a view
                FrameworkElement frameworkElement = e.NewItems[0] as FrameworkElement;
                DockingManager contentPane;
                if (frameworkElement != null)
                {
                    
                        contentPane = frameworkElement.Parent as DockingManager;
                        if (contentPane != null)
                            contentPane.ActiveWindow = frameworkElement;
                    
                }
                else
                {
                    //must be a viewmodel
                    object viewModel = e.NewItems[0];
                    var fElement = GetContentPaneFromViewModel(viewModel);
                    if (fElement != null)
                    {
                        contentPane = frameworkElement.Parent as DockingManager;
                        contentPane.ActiveWindow = frameworkElement;
                    }
                }
            }
        }

        FrameworkElement GetContentPaneFromViewModel(object viewModel)
        {
            var panes = (HostControl as DockingManager).Children;
            foreach (ContentControl contentcontrol in panes)
            {
                if (contentcontrol.DataContext == viewModel)
                    return contentcontrol as FrameworkElement;
            }

            return null;
        }
    }
}
4

0 回答 0