WPF: template or UserControl with 2 (or more!) ContentPresenters to present content in 'slots'

后端 未结 3 1507
暗喜
暗喜 2020-12-04 08:27

I am developing LOB application, where I will need multiple dialog windows (and displaying everything in one window is not an option/makes no sense).

I would like to

3条回答
  •  我在风中等你
    2020-12-04 09:13

    Hasta la victoria siempre!

    I have come with working solution (first on the internet, it seems to me :))

    The tricky DialogControl.xaml.cs - see comments:

    public partial class DialogControl : UserControl
    {
        public DialogControl()
        {
            InitializeComponent();
    
            //The Logical tree detour:
            // - we want grandchildren to inherit DC from this (grandchildren.DC = this.DC),
            // but the children should have different DC (children.DC = this),
            // so that children can bind on this.Properties, but grandchildren bind on this.DataContext
            this.InnerWrapper.DataContext = this;
            this.DataContextChanged += DialogControl_DataContextChanged;
            // need to reinitialize, because otherwise we will get static collection with all buttons from all calls
            this.Buttons = new ObservableCollection();
        }
    
    
        void DialogControl_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            /* //Heading is ours, we want it to inherit this, so no detour
            if ((this.GetValue(HeadingProperty)) != null)
                this.HeadingContainer.DataContext = e.NewValue;
            */
    
            //pass it on to children of containers: detours
            if ((this.GetValue(ControlProperty)) != null)
                ((FrameworkElement)this.GetValue(ControlProperty)).DataContext = e.NewValue;
    
            if ((this.GetValue(ButtonProperty)) != null)
            {
                foreach (var control in ((ObservableCollection) this.GetValue(ButtonProperty)))
                {
                    control.DataContext = e.NewValue;
                }
            }
        }
    
        public FrameworkElement Control
        {
            get { return (FrameworkElement)this.GetValue(ControlProperty); } 
            set { this.SetValue(ControlProperty, value); }
        }
    
        public ObservableCollection Buttons
        {
            get { return (ObservableCollection)this.GetValue(ButtonProperty); }
            set { this.SetValue(ButtonProperty, value); }
        }
    
        public string Heading
        {
            get { return (string)this.GetValue(HeadingProperty); }
            set { this.SetValue(HeadingProperty, value); }
        }
    
        public static readonly DependencyProperty ControlProperty =
                DependencyProperty.Register("Control", typeof(FrameworkElement), typeof(DialogControl));
        public static readonly DependencyProperty ButtonProperty =
                DependencyProperty.Register(
                    "Buttons",
                    typeof(ObservableCollection),
                    typeof(DialogControl),
                    //we need to initialize this for the designer to work correctly!
                    new PropertyMetadata(new ObservableCollection()));
        public static readonly DependencyProperty HeadingProperty =
                DependencyProperty.Register("Heading", typeof(string), typeof(DialogControl));
    }
    

    And the DialogControl.xaml (no changes):

    
        
            
                
                    
                        
                            
                                
                            
                        
                    
                    
                        
                            
                            
                        
                    
                
            
            
                
                    
            
        
    
    

    Sample usage:

    
        
            
                Edit item
            
            
                
                
                    
                        
                        
                    
                    
                        
                        
                    
    
                    
                    
                    
                    
                
            
            
                
                
                
            
        
    
    
    

提交回复
热议问题