Click here to Skip to main content
15,896,539 members
Articles / Desktop Programming / WPF

WPF Form Designer Prototype (MVVM)

Rate me:
Please Sign up or sign in to vote.
4.83/5 (31 votes)
3 Apr 2013CPOL9 min read 79.7K   3.4K   108  
Form designer with editable display properties and bindings. Generates XAML forms.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Controls.WpfPropertyGrid;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using System.Xml.Serialization;
using SimpleMvvmToolkit;
using WPFFormDesigner.WPFControlUtils;

namespace WPFFormDesigner.WPFPropertyGridProxies
{
    public class ListViewProxy : WPFControlBindingModelBase, IDisposable
    {
        #region Fields

        private ObservableCollection<GridViewColumn> _Columns;
        private ObservableDictionary<string, Type> _ControlTypes;
        private ListView component;
        private DependencyPropertyDescriptor dpdBackground;
        private DependencyPropertyDescriptor dpdFontFamily;
        private DependencyPropertyDescriptor dpdFontSize;
        private DependencyPropertyDescriptor dpdFontStyle;
        private DependencyPropertyDescriptor dpdFontWeight;
        private DependencyPropertyDescriptor dpdForeground;
        private DependencyPropertyDescriptor dpdItemsSource;
        private DependencyPropertyDescriptor dpdSelectedItem;
        private DependencyPropertyDescriptor dpdName;

        #region Constructor

        public ListViewProxy(ListView component)
        {
            if (component == null) throw new ArgumentNullException("component");
            this.component = component;

            CaptureComponent(this.component);

            InitCommands();
        }

        #endregion

        #endregion

        #region Commands

        [XmlIgnore]
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public ICommand AddColumnCommand { get; set; }

        [XmlIgnore]
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public ICommand AddRemoveColumnCommand { get; set; }

        [XmlIgnore]
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public ICommand AddSelectColumnCommand { get; set; }

        [XmlIgnore]
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public ICommand MergeCellsCommand { get; set; }

        [XmlIgnore]
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public ICommand SetColumnTemplateCommand { get; set; }

        private bool CanAddColumn(object obj)
        {
            return true;
        }

        private bool CanAddRemoveColumn(object obj)
        {
            return true;
        }

        private bool CanMergeCells(object obj)
        {
            return true;
        }

        public void InitCommands()
        {
            AddColumnCommand = new RelayCommand<object>(OnAddColumn, CanAddColumn);
            AddRemoveColumnCommand = new RelayCommand<object>(OnAddRemoveColumn, CanAddRemoveColumn);
            MergeCellsCommand = new RelayCommand<object>(MergeCells, CanMergeCells);
            SetColumnTemplateCommand = new RelayCommand<object>(OnSetColumnTemplateCommand, CanSetColumnTemplateCommand);
        }

        private bool CanSetColumnTemplateCommand(object obj)
        {
            return true;
        }

        private void OnSetColumnTemplateCommand(object obj)
        {
            if (SelectedColumnTemplateControlType != null && SelectedColumn != null && SelectedColumn.CellTemplate != null)
            {
                switch(SelectedColumnTemplateControlType.Name)
                {
                    case "ComboBox":
                        BuildComboBoxTemplate();
                        break;
                    case "TextBox":
                        BuildTextBoxTemplate();
                        break;
                    case "TextBlock":
                        BuildTextBlockTemplate();
                        break;
                    case "CheckBox":
                        BuildCheckBoxTemplate();
                        break;
                    case "DatePicker":
                        BuildDatePickerTemplate();
                        break;
                }
            }
        }

        private void BuildDatePickerTemplate()
        {
            var fef = new FrameworkElementFactory(SelectedColumnTemplateControlType);
            //Text binding
            Binding binding = GetGenericBinding();
            fef.SetBinding(DatePicker.SelectedDateProperty, binding);
            SetDataTemplate(fef);
        }

        private void BuildCheckBoxTemplate()
        {
            var fef = new FrameworkElementFactory(SelectedColumnTemplateControlType);
            //Text binding
            Binding binding = GetGenericBinding();

            if (!string.IsNullOrEmpty(SelectedColumnBindingPath) && SelectedColumnBindingPath.EndsWith("Indicator"))
            {
                IValueConverter vc = Application.Current.Resources["IndicatorConverter"] as IValueConverter;
                if (vc != null)
                {
                    binding.Converter = vc;
                }
            }

            fef.SetBinding(CheckBox.IsCheckedProperty, binding);
            SetDataTemplate(fef);
        }
 
        private void BuildTextBoxTemplate()
        {
            var fef = new FrameworkElementFactory(SelectedColumnTemplateControlType);
            //Use SelectedColumnWidth instead of hardcode??
            fef.SetValue(TextBox.WidthProperty,(Double)100);
            fef.SetValue(TextBox.HeightProperty, (Double)20);
            //Text binding
            Binding binding = GetGenericBinding();
            fef.SetBinding(TextBox.TextProperty,binding);
            SetDataTemplate(fef);
        }

        private void BuildTextBlockTemplate()
        {
            var fef = new FrameworkElementFactory(SelectedColumnTemplateControlType);
            //Use SelectedColumnWidth instead of hardcode??
            fef.SetValue(TextBlock.WidthProperty, (Double)100);
            fef.SetValue(TextBlock.HeightProperty, (Double)20);
            //Text binding
            Binding binding = GetGenericBinding();
            fef.SetBinding(TextBlock.TextProperty, binding);
            SetDataTemplate(fef);
        }

        private Binding GetGenericBinding()
        {
            Binding binding = new Binding(SelectedColumnBindingPath);
            binding.Mode = (BindingMode) Enum.Parse(typeof (BindingMode), SelectedColumnTemplateBindingMode);
            binding.UpdateSourceTrigger = (UpdateSourceTrigger)Enum.Parse(typeof(UpdateSourceTrigger), SelectedColumnTemplateBindingUpdateSourceTrigger);
            return binding;
        }

        private void BuildComboBoxTemplate()
        {
            var fef = new FrameworkElementFactory(SelectedColumnTemplateControlType);
            /** If this is a Ref Data Combo then override all previous ComboBox settings and
            * create the default ComboBox binding for all properties in ths ComboBox **/
            if (_IsRefDataCombo)
            {
                //ItemsSource binding                        
                Binding binding = new Binding(SelectedColumnBindingPath);
                IValueConverter vc =
                    Application.Current.Resources["ComboBoxBindingConverter"] as IValueConverter;

                if (vc != null)
                {
                    binding.Converter = vc;
                }
                binding.Mode = BindingMode.Default;
                binding.UpdateSourceTrigger = System.Windows.Data.UpdateSourceTrigger.Default;
                fef.SetBinding(ComboBox.ItemsSourceProperty, binding);

                //Selected Value Binding
                Binding sel_value_binding = new Binding(SelectedColumnBindingPath);
                fef.SetBinding(Selector.SelectedValueProperty, sel_value_binding);
                //Set Display Member Path and Selected Value Path
                fef.SetValue(ComboBox.DisplayMemberPathProperty, "Value");
                fef.SetValue(ComboBox.SelectedValuePathProperty, "Key");
                //Create the DataTemplate
                SetDataTemplate(fef);
            }
            else
            {
                //Text binding
                Binding binding = GetGenericBinding();
                fef.SetBinding(ComboBox.ItemsSourceProperty, binding);
                SetDataTemplate(fef);   
            }
        }

        private void SetDataTemplate(FrameworkElementFactory fef)
        {
            var dt = new DataTemplate();
            dt.VisualTree = null;
            dt.VisualTree = fef;
            dt.Seal();
            SelectedColumn.CellTemplate = dt;
        }

        private void MergeCells(object obj)
        {
            int x = 10;
        }

        private void OnAddColumn(object obj)
        {
            var pg = UIHelper.FindParent<PropertyGrid>(obj as FrameworkElement, false);
            if (pg == null) return;
            var lvp = pg.SelectedObject as ListViewProxy;
            if (lvp == null) return;
            var gv = lvp.Component.View as GridView;
            if (gv == null) return;

            //Create First Dummy Column
            var gvc = new GridViewColumn();
            gvc.Header = "New Column";
            var fef = new FrameworkElementFactory(typeof (TextBox));
            //default width
            fef.SetValue(FrameworkElement.WidthProperty, (Double)100);
            var binding = new Binding("Model.Source");
            binding.Mode = BindingMode.TwoWay;
            binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
            fef.SetBinding(TextBox.TextProperty, binding);
            var dt = new DataTemplate();
            dt.VisualTree = fef;
            dt.Seal();
            gvc.CellTemplate = dt;
            lvp.AddColumn(gvc);

            //Set SelectedColumnKVP to Last item
            SelectedColumnKVP = ColumnList.Last();

            //Set the VM default properties for this new column
            SelectedColumnTemplateControlType = typeof (TextBox);
            SelectedColumnTemplateBindingMode = binding.Mode.ToString();
            SelectedColumnTemplateBindingUpdateSourceTrigger = binding.UpdateSourceTrigger.ToString();

            //Notify SelectedColumnHeader changes
            NotifyPropertyChanged(x => SelectedColumnHeader);
            NotifyPropertyChanged(x => SelectedColumnBindingPath);
            //Expand the Column Settings expander
            IsColumnSettingsExpanded = true;
        }

        private void OnAddRemoveColumn(object obj)
        {
            //var pg = UIHelper.FindParent<PropertyGrid>(obj as FrameworkElement, false);
            var pg = obj as PropertyGrid;
            if (pg == null) return;
            var lvp = pg.SelectedObject as ListViewProxy;
            if (lvp == null) return;
            var gv = lvp.Component.View as GridView;
            if (gv == null) return;
            var gvc = new GridViewColumn();
            gvc.Header = "Remove";
            var fef = new FrameworkElementFactory(typeof (Button));
            fef.SetValue(ContentControl.ContentProperty, "Remove");
            var binding = new Binding("DataContext.RemoveObservableCollectionItemCommand");
            var rs = new RelativeSource(RelativeSourceMode.FindAncestor);
            rs.AncestorType = typeof (UserControl);
            binding.RelativeSource = rs;
            fef.SetBinding(ButtonBase.CommandProperty, binding);
            //Send the whole ObservableCollection as parameter
            //CommandParameter="{Binding Path=ItemsSource, RelativeSource={RelativeSource FindAncestor, AncestorType=ListView}}"
            Binding parameter_binding = new Binding("ItemsSource");
            rs = new RelativeSource(RelativeSourceMode.FindAncestor);
            rs.AncestorType = typeof (ListView);
            parameter_binding.RelativeSource = rs;
            fef.SetBinding(ButtonBase.CommandParameterProperty, parameter_binding);
            var dt = new DataTemplate();
            dt.VisualTree = fef;
            dt.Seal();
            gvc.CellTemplate = dt;
            lvp.AddColumn(gvc);
        }

        #endregion

        #region Properties

        private ObservableDictionary<int, GridViewColumn> _ColumnList;
        private bool _IsColumnSettingsExpanded;
        private KeyValuePair<int, GridViewColumn> _SelectedColumnKvp;
        private bool _ShowRemoveButton;

        [XmlIgnore]
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public ListView Component
        {
            get { return component; }
            set { component = value; }
        }

        public FontFamily FontFamily
        {
            get { return component.GetValue(Control.FontFamilyProperty) as FontFamily; }
            set { component.SetValue(Control.FontFamilyProperty, value); }
        }

        public FontStyle FontStyle
        {
            get { return (FontStyle) component.GetValue(Control.FontStyleProperty); }
            set { component.SetValue(Control.FontStyleProperty, value); }
        }

        public double FontSize
        {
            get { return (double) component.GetValue(Control.FontSizeProperty); }
            set { component.SetValue(Control.FontSizeProperty, value); }
        }

        public FontWeight FontWeight
        {
            get { return (FontWeight) component.GetValue(Control.FontWeightProperty); }
            set { component.SetValue(Control.FontWeightProperty, value); }
        }

        public SolidColorBrush Background
        {
            get { return component.GetValue(Control.BackgroundProperty) as SolidColorBrush; }
            set { component.SetValue(Control.BackgroundProperty, value); }
        }

        public SolidColorBrush Foreground
        {
            get { return component.GetValue(Control.ForegroundProperty) as SolidColorBrush; }
            set { component.SetValue(Control.ForegroundProperty, value); }
        }

        public object SelectedItem
        {
            get { return component.GetValue(ListView.SelectedItemProperty); }
            set { component.SetValue(ListView.SelectedItemProperty, value); }
        }

        public string Name
        {
            get { return component.GetValue(ListView.NameProperty) as string; }
            set { component.SetValue(ListView.NameProperty, value); }
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public IEnumerable ItemsSource
        {
            get { return component.GetValue(ItemsControl.ItemsSourceProperty) as IEnumerable; }
            set { component.SetValue(ItemsControl.ItemsSourceProperty, value); }
        }

        public ObservableCollection<GridViewColumn> Columns
        {
            get
            {
                var gv = component.View as GridView;
                if (gv != null && _Columns == null)
                {
                    _Columns = gv.Columns;
                }
                return _Columns;
            }
            set
            {
                _Columns = value;
                NotifyPropertyChanged(m => Columns);
            }
        }

        [XmlIgnore]
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public KeyValuePair<int, GridViewColumn> SelectedColumnKVP
        {
            get { return _SelectedColumnKvp; }
            set
            {
                _SelectedColumnKvp = value;
                _SelectedColumnBindingPath = null;
                _SelectedColumnTemplateControlType = null;
                _SelectedColumnTemplateBindingMode = null;
                _SelectedColumnTemplateBindingUpdateSourceTrigger = null;
                NotifyPropertyChanged(m => SelectedColumnKVP);
                NotifyPropertyChanged(x => SelectedColumnHeader);
                NotifyPropertyChanged(x => SelectedColumnBindingPath);
                NotifyPropertyChanged(x => SelectedColumnTemplateControlType);
                NotifyPropertyChanged(x => SelectedColumnTemplateBindingMode);
                NotifyPropertyChanged(x => SelectedColumnTemplateBindingUpdateSourceTrigger);
            }
        }

        [XmlIgnore]
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public GridViewColumn SelectedColumn
        {
            get { return SelectedColumnKVP.Value; }
        }

        private string _SelectedColumnTemplateBindingMode;

        [XmlIgnore]
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public string SelectedColumnTemplateBindingMode
        {
            get
            {
                //if (_SelectedColumnTemplateBindingMode == null)
                //    _SelectedColumnTemplateBindingMode = BindingMode.Default.ToString();
                //_SelectedColumnTemplateBindingMode = BindingMode.Default.ToString();
                if (_SelectedColumnTemplateBindingMode == null && SelectedColumn != null && SelectedColumn.CellTemplate != null)
                {
                    DataTemplate dt = SelectedColumn.CellTemplate;
                    var fe = dt.LoadContent() as FrameworkElement;
                    if (fe != null)
                    {
                        Binding binding = BindingOperations.GetBinding(fe, fe.GetDependencyPropertyFromControl());
                        if (binding != null)
                        {
                            _SelectedColumnTemplateBindingMode = binding.Mode.ToString();
                        }
                    }
                }
                return _SelectedColumnTemplateBindingMode;
            }
            set
            {
                //if (SelectedColumn != null && SelectedColumn.CellTemplate != null &&
                //    SelectedColumnTemplateControlType != null)
                //{
                //    var fef = new FrameworkElementFactory(SelectedColumnTemplateControlType);

                //    var binding = new Binding(SelectedColumnBindingPath);
                //    binding.Mode = (BindingMode) Enum.Parse(typeof (BindingMode), value);
                //    binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
                //    fef.SetBinding(SelectedColumnTemplateControlType.GetDependencyPropertyFromControl(), binding);

                //    var dt = new DataTemplate();
                //    dt.VisualTree = null;
                //    dt.VisualTree = fef;
                //    dt.Seal();
                //    SelectedColumn.CellTemplate = dt;
                _SelectedColumnTemplateBindingMode = value;
                NotifyPropertyChanged(x => SelectedColumnTemplateBindingMode);
                //}
            }
        }

        private string _SelectedColumnTemplateBindingUpdateSourceTrigger;

        [XmlIgnore]
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public string SelectedColumnTemplateBindingUpdateSourceTrigger
        {
            get
            {
                //if (_SelectedColumnTemplateBindingUpdateSourceTrigger == null)
                //    _SelectedColumnTemplateBindingUpdateSourceTrigger = UpdateSourceTrigger.Default.ToString();
                
                //_SelectedColumnTemplateBindingUpdateSourceTrigger = UpdateSourceTrigger.Default.ToString();
                if (_SelectedColumnTemplateBindingUpdateSourceTrigger == null && SelectedColumn != null && SelectedColumn.CellTemplate != null)
                {
                    DataTemplate dt = SelectedColumn.CellTemplate;
                    var fe = dt.LoadContent() as FrameworkElement;
                    if (fe != null)
                    {
                        Binding binding = BindingOperations.GetBinding(fe, fe.GetDependencyPropertyFromControl());
                        if (binding != null)
                        {
                            _SelectedColumnTemplateBindingUpdateSourceTrigger = binding.UpdateSourceTrigger.ToString();
                        }
                    }
                }
                return _SelectedColumnTemplateBindingUpdateSourceTrigger;
            }
            set
            {
                //if (SelectedColumn != null && SelectedColumn.CellTemplate != null &&
                //    SelectedColumnTemplateControlType != null)
                //{
                //    DataTemplate dt = SelectedColumn.CellTemplate;
                //    var fe = dt.LoadContent() as FrameworkElement;
                //    if (fe != null)
                //    {
                //        var fef = new FrameworkElementFactory(SelectedColumnTemplateControlType);

                //        Binding binding = BindingOperations.GetBinding(fe, fe.GetDependencyPropertyFromControl());
                //        if (binding == null)
                //        {

                //            binding = new Binding(SelectedColumnBindingPath);
                //            binding.Mode =
                //                (BindingMode) Enum.Parse(typeof (BindingMode), SelectedColumnTemplateBindingMode);
                //            binding.UpdateSourceTrigger =
                //                (UpdateSourceTrigger) Enum.Parse(typeof (UpdateSourceTrigger), value);
                //        }
                //        else
                //        {
                //            binding.UpdateSourceTrigger =
                //                (UpdateSourceTrigger) Enum.Parse(typeof (UpdateSourceTrigger), value);
                //        }
                //        fef.SetBinding(SelectedColumnTemplateControlType.GetDependencyPropertyFromControl(), binding);

                //var dt = new DataTemplate();
                //dt.VisualTree = null;
                //dt.VisualTree = fef;
                //dt.Seal();
                //SelectedColumn.CellTemplate = dt;
                _SelectedColumnTemplateBindingUpdateSourceTrigger = value;
                NotifyPropertyChanged(x => SelectedColumnTemplateBindingUpdateSourceTrigger);
                //}
                //}
            }
        }

        private bool _IsRefDataCombo;

        [XmlIgnore]
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public bool IsRefDataCombo
        {
            get { return _IsRefDataCombo; }
            set
            {
                _IsRefDataCombo = value;
                NotifyPropertyChanged(m => IsRefDataCombo);
                
            }
        }

        private string _SelectedColumnBindingPath;
        [XmlIgnore]
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public string SelectedColumnBindingPath
        {
            get
            {
                //_SelectedColumnBindingPath = null;
                if (_SelectedColumnBindingPath == null && SelectedColumn != null && 
                    SelectedColumn.CellTemplate != null)
                {
                    DataTemplate dt = SelectedColumn.CellTemplate;

                    var fe = dt.LoadContent() as FrameworkElement;
                    if (fe != null)
                    {
                        Binding binding = BindingOperations.GetBinding(fe, fe.GetDependencyPropertyFromControl());
                        if (binding != null)
                        {
                            _SelectedColumnBindingPath = binding.Path.Path;
                        }
                    }
                }
                return _SelectedColumnBindingPath;
            }
            set
            {
                //if (SelectedColumn != null && SelectedColumn.CellTemplate != null && !string.IsNullOrEmpty(value))
                //{
                //    DataTemplate dt = SelectedColumn.CellTemplate;
                //    var fe = dt.LoadContent() as FrameworkElement;
                //    if (fe != null)
                //    {
                //        Binding binding = BindingOperations.GetBinding(fe, fe.GetDependencyPropertyFromControl());
                //        if (binding != null)
                //        {
                //            binding.Path.Path = value;
                            _SelectedColumnBindingPath = value;
                            NotifyPropertyChanged(m => SelectedColumnBindingPath);
                       // }
                    //}
               // }
            }
        }

        private double _SelectedColumnWidth;

        [XmlIgnore]
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public double SelectedColumnWidth
        {
            get
            {
                //if (SelectedColumn != null && SelectedColumn.CellTemplate != null)
                //{
                //    DataTemplate dt = SelectedColumn.CellTemplate;

                //    var fe = dt.LoadContent() as FrameworkElement;
                //    if (fe != null)
                //    {
                //        return fe.Width;
                //    }
                //}
                return _SelectedColumnWidth;
            }
            set
            {
                _SelectedColumnWidth = value;
                NotifyPropertyChanged(m => SelectedColumnWidth);
            }
        }


        [XmlIgnore]
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public string SelectedColumnHeader
        {
            get
            {
                if (SelectedColumn != null)
                {
                    return SelectedColumn.Header.ToString();
                }
                return null;
            }
            set
            {
                if (SelectedColumn != null)
                {
                    SelectedColumn.Header = value;
                }
                NotifyPropertyChanged(m => SelectedColumnHeader);
            }
        }

        [XmlIgnore]
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public ObservableDictionary<int, GridViewColumn> ColumnList
        {
            get
            {
                if (_ColumnList == null)
                {
                    _ColumnList = new ObservableDictionary<int, GridViewColumn>();
                }
                _ColumnList.Clear();
                //Populate columns
                for (int i = 0; i < Columns.Count; i++)
                {
                    _ColumnList.Add(i, Columns[i]);
                }
                return _ColumnList;
            }
            set
            {
                _ColumnList = value;
                NotifyPropertyChanged(x => ColumnList);
            }
        }


        [XmlIgnore]
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public ObservableDictionary<string, Type> ControlTypes
        {
            get
            {
                if (_ControlTypes == null)
                {
                    _ControlTypes = new ObservableDictionary<string, Type>();
                    _ControlTypes.Add("TextBox", typeof (TextBox));
                    _ControlTypes.Add("TextBlock", typeof (TextBlock));
                    _ControlTypes.Add("CheckBox", typeof (CheckBox));
                    _ControlTypes.Add("ComboBox", typeof (ComboBox));
                    _ControlTypes.Add("Button", typeof (Button));
                }
                return _ControlTypes;
            }
            set
            {
                _ControlTypes = value;
                NotifyPropertyChanged(m => ControlTypes);
            }
        }

        private Type _SelectedColumnTemplateControlType;
        [XmlIgnore]
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public Type SelectedColumnTemplateControlType
        {
            get
            {
                //_SelectedColumnTemplateControlType = null;
                if (_SelectedColumnTemplateControlType == null && SelectedColumn != null &&
                    SelectedColumn.CellTemplate != null)
                {
                    DataTemplate dt = SelectedColumn.CellTemplate;
                    if (dt.VisualTree != null)
                        _SelectedColumnTemplateControlType = dt.VisualTree.Type;
                    else
                    {
                        _SelectedColumnTemplateControlType = dt.LoadContent().GetType();
                    }
                }
                return _SelectedColumnTemplateControlType;
            }
            set
            {
                //if (value != null)
                //{
                //    var fef = new FrameworkElementFactory(value);

                //    DataTemplate dt = null;
                //    if (SelectedColumn != null && SelectedColumn.CellTemplate != null)
                //    {
                //        dt = SelectedColumn.CellTemplate;
                //        DependencyObject obj = dt.LoadContent();
                //        if (obj != null &&
                //            BindingOperations.GetBinding(obj, value.GetDependencyPropertyFromControl()) != null)
                //        {
                //        }
                //        else
                //        {
                //            var binding = new Binding(SelectedColumnBindingPath);
                //            binding.Mode =
                //                (BindingMode) Enum.Parse(typeof (BindingMode), SelectedColumnTemplateBindingMode);
                //            binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
                //            fef.SetBinding(value.GetDependencyPropertyFromControl(), binding);
                //            dt = new DataTemplate();
                //            dt.VisualTree = fef;
                //            dt.Seal();
                //            SelectedColumn.CellTemplate = dt;
                //        }
                //    }
                    _SelectedColumnTemplateControlType = value;
                    NotifyPropertyChanged(m => SelectedColumnTemplateControlType);
                //}
            }
        }

        [XmlIgnore]
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public bool IsColumnSettingsExpanded
        {
            get { return _IsColumnSettingsExpanded; }
            set
            {
                _IsColumnSettingsExpanded = value;
                NotifyPropertyChanged(m => IsColumnSettingsExpanded);
            }
        }

        [XmlIgnore]
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public bool ShowRemoveButton
        {
            get { return _ShowRemoveButton; }
            set
            {
                _ShowRemoveButton = value;
                NotifyPropertyChanged(m => ShowRemoveButton);
            }
        }

        [XmlIgnore]
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public bool IsColumnComboEnabled
        {
            get { return (Columns != null && Columns.Count > 0); }
        }

        #endregion

        private bool disposed;

        #region IDisposable Members

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        #endregion

        public void AddColumn(GridViewColumn gvc)
        {
            Columns.Add(gvc);
            NotifyPropertyChanged(x => ColumnList);
            NotifyPropertyChanged(x => IsColumnComboEnabled);
        }

        private void CaptureComponent(ListView listview)
        {
            dpdFontFamily = DependencyPropertyDescriptor.FromProperty(Control.FontFamilyProperty, typeof (ListView));
            dpdFontFamily.AddValueChanged(listview, OnFontFamilyChanged);

            dpdFontStyle = DependencyPropertyDescriptor.FromProperty(Control.FontStyleProperty, typeof (ListView));
            dpdFontStyle.AddValueChanged(listview, OnFontStyleChanged);

            dpdFontSize = DependencyPropertyDescriptor.FromProperty(Control.FontSizeProperty, typeof (ListView));
            dpdFontSize.AddValueChanged(listview, OnFontSizeChanged);

            dpdFontWeight = DependencyPropertyDescriptor.FromProperty(Control.FontWeightProperty, typeof (ListView));
            dpdFontWeight.AddValueChanged(listview, OnFontWeightChanged);

            dpdBackground = DependencyPropertyDescriptor.FromProperty(Control.BackgroundProperty, typeof (ListView));
            dpdBackground.AddValueChanged(listview, OnBackgroundChanged);

            dpdForeground = DependencyPropertyDescriptor.FromProperty(Control.ForegroundProperty, typeof (ListView));
            dpdForeground.AddValueChanged(listview, OnForegroundChanged);

            dpdItemsSource = DependencyPropertyDescriptor.FromProperty(ItemsControl.ItemsSourceProperty,
                                                                       typeof (ListView));

            dpdSelectedItem = DependencyPropertyDescriptor.FromProperty(Selector.SelectedItemProperty, typeof (ListView));
            dpdSelectedItem.AddValueChanged(listview, OnSelectedItemChanged);

            dpdName = DependencyPropertyDescriptor.FromProperty(ListView.NameProperty, typeof(ListView));
            dpdName.AddValueChanged(listview, OnNameChanged);

            dpdForeground.AddValueChanged(listview, OnItemsSourceChanged);
        }

        private void Dispose(bool disposing)
        {
            if (disposed) return;

            if (disposing)
            {
                ReleaseComponent(component);
            }

            disposed = true;
        }

        ~ListViewProxy()
        {
            Dispose(false);
        }

        private void OnBackgroundChanged(object sender, EventArgs e)
        {
            NotifyPropertyChanged(x => Background);
        }

        private void OnFontFamilyChanged(object sender, EventArgs e)
        {
            NotifyPropertyChanged(x => FontFamily);
        }

        private void OnFontSizeChanged(object sender, EventArgs e)
        {
            NotifyPropertyChanged(x => FontSize);
        }

        private void OnFontStyleChanged(object sender, EventArgs e)
        {
            NotifyPropertyChanged(x => FontStyle);
        }

        private void OnFontWeightChanged(object sender, EventArgs e)
        {
            NotifyPropertyChanged(x => FontWeight);
        }

        private void OnForegroundChanged(object sender, EventArgs e)
        {
            NotifyPropertyChanged(x => Foreground);
        }

        private void OnItemsSourceChanged(object sender, EventArgs e)
        {
            NotifyPropertyChanged(x => ItemsSource);
        }

        private void OnSelectedItemChanged(object sender, EventArgs e)
        {
            NotifyPropertyChanged(x => SelectedItem);
        }

        private void OnNameChanged(object sender, EventArgs e)
        {
            NotifyPropertyChanged(x => Name);
        }

        private void ReleaseComponent(ListView listview)
        {
            if (dpdFontFamily != null)
            {
                dpdFontFamily.RemoveValueChanged(listview, OnFontFamilyChanged);
                dpdFontFamily = null;
            }

            if (dpdFontStyle != null)
            {
                dpdFontStyle.RemoveValueChanged(listview, OnFontStyleChanged);
                dpdFontStyle = null;
            }

            if (dpdFontSize != null)
            {
                dpdFontSize.RemoveValueChanged(listview, OnFontSizeChanged);
                dpdFontSize = null;
            }

            if (dpdFontWeight != null)
            {
                dpdFontWeight.RemoveValueChanged(listview, OnFontWeightChanged);
                dpdFontWeight = null;
            }

            if (dpdBackground != null)
            {
                dpdBackground.RemoveValueChanged(listview, OnBackgroundChanged);
                dpdBackground = null;
            }

            if (dpdForeground != null)
            {
                dpdForeground.RemoveValueChanged(listview, OnForegroundChanged);
                dpdForeground = null;
            }

            if (dpdItemsSource != null)
            {
                dpdItemsSource.RemoveValueChanged(listview, OnItemsSourceChanged);
                dpdItemsSource = null;
            }

            if (dpdSelectedItem != null)
            {
                dpdSelectedItem.RemoveValueChanged(listview, OnSelectedItemChanged);
                dpdSelectedItem = null;
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
Sr. application developer currently developing desktop and web applications for the specialty insurance sector. Data integration specialist, interested in learning the latest .Net technologies.

Comments and Discussions