using System;
using System.ComponentModel.Composition;
using System.Windows;
using System.Windows.Data;

namespace TaskIt.Infrastructure
{
    [Export]
    public class DataContextProxy : FrameworkElement
    {
        public DataContextProxy()
        {
            Loaded += OnLoaded;
        }

        void OnLoaded(object sender, RoutedEventArgs e)
        {
            var binding = new Binding();
            if (!String.IsNullOrEmpty(BindingPropertyName))
            {
                binding.Path = new PropertyPath(BindingPropertyName);
            }
            binding.Source = DataContext;
            binding.Mode = BindingMode;
            SetBinding(ViewModelProperty, binding);
        }

        public Object ViewModel
        {
            get { return GetValue(ViewModelProperty); }
            set { SetValue(ViewModelProperty, value); }
        }

        public static readonly DependencyProperty ViewModelProperty =
            DependencyProperty.Register("ViewModel", typeof(Object), typeof(DataContextProxy), null);

        public string BindingPropertyName { get; set; }

        public BindingMode BindingMode { get; set; }
    }
}
