How to Achieve Lazy Binding of Tab Page Controls in WPF?

前端 未结 3 1310
庸人自扰
庸人自扰 2020-12-29 16:28

I have an entity class. This entity has lots of properties and entity\'s data is shown to the user in several TabItems of a TabControl. I also impl

3条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-29 17:03

    I created this solution which works for our third party tabcontrol.

    The idea is to "intercept" the datacontext when it is beeing set, save it for later and set the datacontext back to null. When the tabitem gets focus we then set the datacontext and the data will populate in the tab.

    Implemented as a dependency property. Then simply set the property on the tabs that need to (do not set it on the tab that comes up as a default)

        #region SavedDataContext
    
        private static object GetSavedDataContext(TabItemEx tabItem)
        {
            return tabItem.GetValue(SavedDataContextProperty);
        }
    
        private static void SetSavedDataContext(TabItemEx tabItem, object value)
        {
            tabItem.SetValue(SavedDataContextProperty, value);
        }
    
        public static readonly DependencyProperty SavedDataContextProperty =
            DependencyProperty.RegisterAttached("SavedDataContext", typeof(object),
                                                typeof(Attach), new UIPropertyMetadata(null));
    
        #endregion
    
        #region LazyLoad
    
        public static bool GetLazyLoad(TabItemEx tabItem)
        {
            return (bool)tabItem.GetValue(LazyLoadProperty);
        }
    
        public static void SetLazyLoad(TabItemEx tabItem, bool value)
        {
            tabItem.SetValue(LazyLoadProperty, value);
        }
    
        private static readonly DependencyProperty LazyLoadProperty =
            DependencyProperty.RegisterAttached("LazyLoad", typeof(bool),
                                                typeof(Attach), new UIPropertyMetadata(false, LazyLoadPropertyChanged));
    
        private static void LazyLoadPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs eventArgs)
        {
            if ((bool)eventArgs.NewValue)
            {
                var tabItemEx = sender as TabItemEx;
                if (tabItemEx == null)
                    return;
    
                tabItemEx.DataContextChanged += DataContextChanged;
                tabItemEx.GotFocus += TabGotFocus;
            }
        }
    
    
        #endregion
    
        private static void TabGotFocus(object sender, RoutedEventArgs e)
        {
            var tabItemEx = sender as TabItemEx;
    
            if (tabItemEx == null)
                return;
    
            tabItemEx.GotFocus -= TabGotFocus;
            tabItemEx.DataContext = GetSavedDataContext(tabItemEx);
            tabItemEx.IsSelected = true;
        }
    
        private static void DataContextChanged(object sender, DependencyPropertyChangedEventArgs eventArgs)
        {
            var tabItemEx = sender as TabItemEx;
    
            if (tabItemEx == null)
                return;
    
            SetSavedDataContext(tabItemEx, eventArgs.NewValue);
    
            tabItemEx.DataContextChanged -= DataContextChanged;
            tabItemEx.DataContext = null;
        }
    

提交回复
热议问题