Bindings inside CompositeCollection

后端 未结 1 1565
盖世英雄少女心
盖世英雄少女心 2021-01-18 08:38

I want to create a TabControl with a number of \"static\" TabItems (explicitly typed in XAML) and a number of dynamically added TabItems. To achieve this I tried to use a Co

1条回答
  •  盖世英雄少女心
    2021-01-18 09:33

    I had a similar scenario. To solve this I found the following article.

    This post explains how to create a freezable proxy object that you can set your data context to. You then add this proxy as a resource that can be referenced as a static resource. See the following:

    public class BindingProxy : Freezable
    {
        public static DependencyProperty DataContextProperty = DependencyProperty.Register(
            "DataContext", typeof(object), typeof(BindingProxy), new UIPropertyMetadata(null));
    
        public object DataContext
        {
            get { return GetValue(DataContextProperty); }
            set { SetValue(DataContextProperty, value); }
        }
    
        protected override Freezable CreateInstanceCore()
        {
            return new BindingProxy();
        }
    }
    

    This can then be used in your xaml like this:

    
      
        
          Text1
          Text2
        
        
      
    
      
        
          
            
              
                
                
              
            
            
          
        
      
    
    

    0 讨论(0)
提交回复
热议问题