This is a migrated thread and some comments may be shown as answers.

selectedvalue change event for custom control for GridViewComboColumn

0 Answers 54 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Hazzard
Top achievements
Rank 1
Hazzard asked on 10 Nov 2011, 02:18 AM
the custom control below is working well for displaying values in the dropdown.  But what it IS NOT doing is saving changes a user makes when they select an item in the dropdown.  What would I have to add to achieve that?  Thank you.
    public class IWGridViewComboColumn : GridViewComboBoxColumn
    {
        public String Text
        {
            get
            {
                return (String)GetValue(TextProperty);
            }
            set
            {
                SetValue(TextProperty, value);
            }
        }
  
        public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text",
            typeof(String),
            typeof(IWGridViewComboColumn),
            new PropertyMetadata(null));
  
        public IWGridViewComboColumn()
        {
  
        }
  
        public override FrameworkElement CreateCellEditElement(GridViewCell cell, object dataItem)
        {
            base.CreateCellEditElement(cell, dataItem);
            var currentcontrol = cell.Content;
            if (currentcontrol != null && currentcontrol.GetType() == typeof(ComboBox))
                Text = ((ComboBox)currentcontrol).SelectedValue.ToString();
  
            StackPanelWComboBox panel = null;
              
            if (this.DataMemberBinding != null)
            {
                this.BindingTarget = StackPanelWComboBox.TextProperty;
            }
  
            if (panel == null)
            {
                AppliedStandardsModel app_std_Model = (AppliedStandardsModel)dataItem;
                if (app_std_Model.Class != "NA")
                {
                    panel = new StackPanelWComboBox(dataItem, ActualWidth, MinWidth);
                }
            }
  
            if (this.DataMemberBinding != null)
            {
                panel.SetBinding(this.BindingTarget, this.CreateValueBinding());
            }
  
            panel.Text = Text;
  
            return panel as FrameworkElement;
        }
        public override FrameworkElement CreateCellElement(GridViewCell cell, object dataItem)
        {
            base.CreateCellElement(cell, dataItem);
            var currentcontrol = cell.Content;
            StackPanelWComboBox panel = null;
            if (currentcontrol != null)
            {
                panel = cell.Content as StackPanelWComboBox;
            }            
  
            if (this.DataMemberBinding != null)
            {
                this.BindingTarget = StackPanelWComboBox.TextProperty;
            }
  
            if (panel == null)
            {
                AppliedStandardsModel app_std_Model = (AppliedStandardsModel)dataItem;
                if (app_std_Model.Class != "NA") //Don't even show user combobox, just leave cell blank.
                {
                    panel = new StackPanelWComboBox(dataItem, 90, 90);
                }
            }
  
            if (this.DataMemberBinding != null)
            {
                panel.SetBinding(this.BindingTarget, this.CreateValueBinding());
            }
              
            return panel;
              
  
        }
        private Binding CreateValueBinding()
        {
            var valueBinding = new Binding();
            valueBinding.Mode = BindingMode.TwoWay;
            valueBinding.NotifyOnValidationError = true;
            valueBinding.ValidatesOnExceptions = true;
            valueBinding.UpdateSourceTrigger = UpdateSourceTrigger.Explicit;
            valueBinding.Path = new PropertyPath(this.DataMemberBinding.Path.Path);
            return valueBinding;
        }
  
        public override void CopyPropertiesFrom(GridViewColumn source)
        {
            base.CopyPropertiesFrom(source);
            var gridViewEasyEntryTextColumn = source as IWGridViewComboColumn;
            if (gridViewEasyEntryTextColumn != null)
            {
                this.Text = gridViewEasyEntryTextColumn.Text;
            }
        }
  
        public override IList<ValidationError> UpdateSourceWithEditorValue(GridViewCell gridViewCell)
        {
            List<ValidationError> errors = new List<ValidationError>();
            StackPanelWComboBox editor = gridViewCell.GetEditingElement() as StackPanelWComboBox;
            BindingExpression bindingExpression = editor.ReadLocalValue(StackPanelWComboBox.TextProperty) as BindingExpression;
            if (bindingExpression != null)
            {
                bindingExpression.UpdateSource();
                errors.AddRange(Validation.GetErrors(editor));
            }
            return errors;
        }
  
        public override object GetNewValueFromEditor(object editor)
        {
            StackPanelWComboBox panel = editor as StackPanelWComboBox;
            if (panel != null)
            {
                panel.Text = panel.tb.SelectedValue.ToString();
                return panel.tb.SelectedValue.ToString();
            }
            else
            {
                return null;
            }
        }
    }
}

public class StackPanelWComboBox : StackPanel
   {
        public ComboBox tb = null;
        //public Button button = null;
         
       #region Dependency Properties
         
        /// <summary>
        /// End ShowLabel Begin Click
        /// </summary>
       public static readonly DependencyProperty
            ClickProperty = DependencyProperty.Register(
            "Click",                                     // property name
            typeof(RoutedEventHandler),                  // type of property
            typeof(StackPanelWComboBox),                 // type of provider
            new PropertyMetadata(ClickChanged));         // Callback invoked on property value has changes.
       public RoutedEventHandler Click
       {
           get { return (RoutedEventHandler)GetValue(ClickProperty); }
           set { SetValue(ClickProperty, value); }
       }
       private static void ClickChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
       {
            
       }
       
       /// <summary>
       /// End Click Begin Text
       /// </summary>
       public static readonly DependencyProperty
            TextProperty = DependencyProperty.Register(
            "Text",                   //property name
            typeof(String),                  //type of property
            typeof(StackPanelWComboBox),  //type of provider
            new PropertyMetadata(TextChanged));  //Callback invoked on property value has changes.
       public String Text
       {
           get { return (String)GetValue(TextProperty); }
           set { SetValue(TextProperty, value); }
       }
       private static void TextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
       {
           StackPanelWComboBox sh = (StackPanelWComboBox)d;
           if (sh.tb == null || e.NewValue == null)
               return;
           sh.tb.SelectedValue = e.NewValue.ToString();
       }
       /// <summary>
       /// end text
       /// </summary>
          
        
       #endregion
       public object CommandParameter
       {
           set
               if (tb == null)
                   return;
               tb.Tag = value;
           }
           get{   
               if (tb == null)
                   return null;
               return tb.Tag;
           }
       }
       public StackPanelWComboBox()
       {
         
       }
       public StackPanelWComboBox(object dataItem, double width, double minwidth)
       {
           createContents(dataItem, width, minwidth);
       }
       public List<EMC_Class> ABCD
       {
           get
           {
               List<EMC_Class> lst = new List<EMC_Class>();
               try
               {
                   foreach (var x in new EMC_Class[] 
                   {
                       new EMC_Class { ClassID = 0, ClassName = "A" },
                       new EMC_Class { ClassID = 1, ClassName = "B" },
                       new EMC_Class { ClassID = 2, ClassName = "C" },
                       new EMC_Class { ClassID = 3, ClassName = "D" }
                   })
                   {
                       lst.Add(x);
                   }
                   return lst;
               }
               catch (Exception ex)
               {
                   throw;
               }
               return lst;
           }
       }
       public List<EMC_Class> AB
       {
           get
           {
               List<EMC_Class> lst = new List<EMC_Class>();
               try
               {
                   foreach (var x in new EMC_Class[] 
                   {
                       new EMC_Class { ClassID = 0, ClassName = "A" },
                       new EMC_Class { ClassID = 1, ClassName = "B" },
                   })
                   {
                       lst.Add(x);
                   }
                   return lst;
               }
               catch (Exception ex)
               {
                   throw;
               }
               return lst;
           }
       }
       public List<EMC_Class> NA
       {
           get
           {
               List<EMC_Class> lst = new List<EMC_Class>();
               try
               {
                   foreach (var x in new EMC_Class[] 
                   {
                       new EMC_Class { ClassID = 0, ClassName = " " },
                   })
                   {
                       lst.Add(x);
                   }
                   return lst;
               }
               catch (Exception ex)
               {
                   throw;
               }
               return lst;
           }
       }
       public class EMC_Class
       {
           public int ClassID { get; set; }
           public string ClassName { get; set; }
       }
  
       public void createContents(object dataItem, double width, double minwidth)
       {
           Children.Clear();
           Orientation = Orientation.Horizontal;
           HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
           tb = new ComboBox();
           tb.Width = width;
           tb.MinWidth = width;
           AppliedStandardsModel app_std_Model = (AppliedStandardsModel)dataItem;
           if (app_std_Model.ClassUserSelected != null)
           {
               tb.DisplayMemberPath = "ClassName";
               tb.SelectedValuePath = "ClassUserSelected";
               tb.SelectedValue = app_std_Model.ClassUserSelected;
           }
           switch (app_std_Model.Class)
           {
               case "NA":
                   tb.ItemsSource = NA;   
                   break;
              case "A or B":
                   tb.ItemsSource = AB;
                  break;
               case "A, B, C, D":
                  tb.ItemsSource = ABCD;
                 break;
             }
           Children.Add(tb);
  
       }
   }

No answers yet. Maybe you can help?

Tags
GridView
Asked by
Hazzard
Top achievements
Rank 1
Share this question
or