Exposing inner Control properties for binding in WPF

后端 未结 2 1877
离开以前
离开以前 2020-12-04 15:44

[Edit]: I figured out how to do this on my own. I posted my solution in the hope that it will save someone else a few days of Googling. If you are a WPF guru, please lo

2条回答
  •  无人及你
    2020-12-04 16:21

    I ended up figuring how how to do this on my own. I'm posting the answer here so that others can see a solution that works, and maybe a WPF guru will come by and show me a better/more elegant way to do this.

    So, the answer ended up being #2. Exposing the inner properties turns out to be the right answer. Setting it up is actually pretty easy.. once you know how to do it. There aren't many complete examples of this (that I could find), so hopefully this one will help someone else that runs into this problem.

    ComboBoxWithLabel.xaml.cs

    The important thing in this file is the use of DependencyProperties. Note that all we're doing right now is just exposing the properties (LabelContent and ItemsSource). The XAML will take care of wiring the internal control's properties to these external properties.

    namespace BoundComboBoxExample
    {
        /// 
        /// Interaction logic for ComboBoxWithLabel.xaml
        /// 
        public partial class ComboBoxWithLabel : UserControl
        {
            // Declare ItemsSource and Register as an Owner of ComboBox.ItemsSource
            // the ComboBoxWithLabel.xaml will bind the ComboBox.ItemsSource to this
            // property
            public IEnumerable ItemsSource
            {
                get { return (IEnumerable)GetValue(ItemsSourceProperty); }
                set { SetValue(ItemsSourceProperty, value); }
            }
    
            public static readonly DependencyProperty ItemsSourceProperty =
              ComboBox.ItemsSourceProperty.AddOwner(typeof(ComboBoxWithLabel));
    
            // Declare a new LabelContent property that can be bound as well
            // The ComboBoxWithLable.xaml will bind the Label's content to this
            public string LabelContent
            {
                get { return (string)GetValue(LabelContentProperty); }
                set { SetValue(LabelContentProperty, value); }
            }
    
            public static readonly DependencyProperty LabelContentProperty =
              DependencyProperty.Register("LabelContent", typeof(string), typeof(ComboBoxWithLabel));
          
            public ComboBoxWithLabel()
            {
                InitializeComponent();
            }
        }
    }
    

    ComboBoxWithLabel.xaml

    The XAML is pretty straightforward, with the exception of the bindings on the Label and the ComboBox ItemsSource. I found that the easiest way to get these bindings right is to declare the properties in the .cs file (as above) and then use the VS2010 designer to setup the binding source from the properties pane. Essentially, this is the only way I know of to bind an inner control's properties to the base control. If there's a better way to do it, please let me know.

    
        
            
                
                
                
        
    
    

    MainWindow.xaml

    The XAML to use this is not interesting at all.. which is exactly what I wanted. You can set the ItemsSource and the LabelContent via all the standard WPF techniques.

    
        
            
        
        
            
        
    
    

    For Completeness Sake, here is the MainWindow.xaml.cs

    /// 
    /// Interaction logic for MainWindow.xaml
    /// 
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            ((ObjectDataProvider)FindResource("LookupValues")).ObjectInstance =
                (from i in Enumerable.Range(0, 5)
                 select string.Format("Bar {0}", i)).ToArray();
    
        }
    }
    

提交回复
热议问题