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

Custom Cell Editor Validation

4 Answers 207 Views
GridView
This is a migrated thread and some comments may be shown as answers.
MiddleTommy
Top achievements
Rank 1
MiddleTommy asked on 02 Dec 2009, 12:03 AM
I have a custom cell editor. when the Cell tries to validate (CellValidating event) the NewValue property is always null.
How can I get the NewValue to be set for validation?

<t:GridViewDataColumn DataMemberBinding="{Binding grade}" Header="Grade" EditTriggers="CellClick"
                        <t:GridViewDataColumn.CellEditTemplate> 
                            <DataTemplate> 
                                <ComboBox IsEditable="True" Text="{Binding grade, UpdateSourceTrigger=LostFocus, Mode=TwoWay}"/> 
                            </DataTemplate> 
                        </t:GridViewDataColumn.CellEditTemplate> 
                    </t:GridViewDataColumn> 

4 Answers, 1 is accepted

Sort by
0
MiddleTommy
Top achievements
Rank 1
answered on 02 Dec 2009, 02:15 PM
I found out that the value is being committed to the source prior to validation. That is why the NewValue is null. I dont know why I didnt think of that before. I am binding directly to the property on the source. How do I bind a custom CellEditor with validation? Is there a property on the Cell that can be bound to?
0
MiddleTommy
Top achievements
Rank 1
answered on 02 Dec 2009, 02:50 PM
I tried binding to the Cell Value but that didnt update the value after editing. It did view the value ok in the editor.

<t:GridViewDataColumn DataMemberBinding="{Binding grade, Mode=TwoWay}" Header="Grade" EditTriggers="CellClick"
                        <t:GridViewDataColumn.CellEditTemplate> 
                            <DataTemplate> 
                                <t:RadComboBox ItemsSource="{Binding AvailableGrades}" IsEditable="{Binding GradeEditable}" Loaded="Editor_Loaded" Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type t:GridViewCell}},Path=Value, Mode=TwoWay, UpdateSourceTrigger=LostFocus}"/> 
                            </DataTemplate> 
                        </t:GridViewDataColumn.CellEditTemplate> 
                    </t:GridViewDataColumn> 

I will keep searching for a solution
0
MiddleTommy
Top achievements
Rank 1
answered on 02 Dec 2009, 10:24 PM
I gave up on creating a custom editor and found it quite easy to create a custom column deriving from the current ComboBoxColumn.

I also added Binding to ItemsSource via ItemsSourceBinding (looked un-finished in telerik's code). Also I added the Ability to switch between Binding the DataMemberBinding to the combobox SelectedValue, SelectedItem or even Text via the ComboBoxBinding Enum property

using System; 
using System.Globalization; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Controls.Primitives; 
using System.Windows.Data; 
using System.Xml; 
using System.Linq; 
using Telerik.Windows.Controls; 
using Telerik.Windows.Data; 
using Telerik.Windows.Controls.GridView; 
using System.Collections; 
using Telerik.Windows.Data.Expressions; 
using Telerik.Windows.Controls.GridView; 
using MyCards.Windows; 
using GridViewColumn=Telerik.Windows.Controls.GridViewColumn; 
 
namespace MyCards.Material 
    /// <summary> 
    /// GridViewComboBoxColumn provides an easy and fast way to display and  
    /// edit lookup data. The key properties to set are : valueMemberPath, DisplayMemberPath and ItemsSource. 
    /// </summary> 
    public class CustomGridViewComboBoxColumn : GridViewComboBoxColumn 
    { 
        /// <summary> 
        ///Copy properties from source column. 
        /// </summary> 
        public override void CopyPropertiesFrom(GridViewColumn source) 
        { 
            base.CopyPropertiesFrom(source); 
 
            var comboColumn = source as CustomGridViewComboBoxColumn; 
            if (comboColumn != null
            { 
                this.SelectedValueMemberPath = comboColumn.SelectedValueMemberPath; 
                this.ItemsSource = comboColumn.ItemsSource; 
                this.ItemsSourceBinding = comboColumn.ItemsSourceBinding; 
                this.DisplayMemberPath = comboColumn.DisplayMemberPath; 
                this.LookupValueConverter = comboColumn.LookupValueConverter; 
            } 
        } 
 
        public DependencyProperty SourceBindingTarget 
        { 
            getset
        } 
 
        /// <summary> 
        /// Represents the IsComboBoxEditable dependency property 
        /// </summary> 
        public static readonly DependencyProperty ComboBoxBindingProperty = 
            DependencyProperty.Register("ComboBoxBinding"
            typeof(ComboBoxBinding), 
            typeof(CustomGridViewComboBoxColumn), 
            null); 
 
        public ComboBoxBinding ComboBoxBinding 
        { 
            get 
            { 
 
                return (ComboBoxBinding)GetValue(ComboBoxBindingProperty); 
            } 
            set 
            { 
                SetValue(ComboBoxBindingProperty, value); 
            } 
        } 
 
        public override FrameworkElement CreateCellElement(GridViewCell cell, object dataItem) 
        { 
            if (this.CellTemplate != null
            { 
                return this.CellTemplate.LoadContent() as FrameworkElement; 
            } 
            else if (DataMemberBinding != null && DataMemberBinding.Path != null
            { 
                base.CreateCellElement(cell, dataItem); 
                var textblock = new TextBlock(); 
 
                var valueBinding = this.DataMemberBinding.CloneBinding(); 
                textblock.SetBinding(TextBlock.TextProperty, valueBinding); 
                return textblock; 
            } 
            return null;  
        } 
 
        /// <summary> 
        /// Creates the element for the cell in edit mode. 
        /// </summary> 
        /// <param name="cell"></param> 
        /// <param name="dataItem"></param> 
        /// <returns></returns> 
        public override FrameworkElement CreateCellEditElement(GridViewCell cell, object dataItem) 
        { 
            FrameworkElement cellElement = null
 
 
            if (this.DataMemberBinding != null && this.DataMemberBinding.Path != null
            { 
                var valueBinding = this.DataMemberBinding.CloneBinding(); 
 
            RadComboBox comboBox = new RadComboBox(); 
            if (ItemsSourceBinding != null
            { 
                this.SourceBindingTarget = RadComboBox.ItemsSourceProperty; 
                comboBox.SetBinding(SourceBindingTarget, ItemsSourceBinding); 
            } 
            else 
                comboBox.ItemsSource = this.ItemsSource; 
            comboBox.DisplayMemberPath = this.DisplayMemberPath; 
            comboBox.SelectedValuePath = this.SelectedValueMemberPath; 
            comboBox.IsEditable = this.IsComboBoxEditable; 
                switch(ComboBoxBinding) 
                { 
                    case ComboBoxBinding.Text: 
                        this.BindingTarget = RadComboBox.TextProperty; 
                        break
                    case Material.ComboBoxBinding.SelectedItem: 
                        this.BindingTarget = RadComboBox.SelectedItemProperty; 
                        break
                    default
                        this.BindingTarget = RadComboBox.SelectedValueProperty; 
                        break
                } 
                comboBox.SetBinding(this.BindingTarget, valueBinding); 
                cellElement = comboBox; 
            } 
             
 
            return cellElement; 
        } 
    } 
 
    public enum ComboBoxBinding 
    { 
        SelectedValue, 
        SelectedItem, 
        Text 
    } 

The CloneBinding method is an internal extension method so you will have to recreate it if you have the source code for the GridView else you will have to ask Telerik to open up the Extention method to be public.
0
Nedyalko Nikolov
Telerik team
answered on 07 Dec 2009, 07:29 AM
Hi TWT,

From you posts I understand that you have discovered the right way to create a custom editor which uses RadGridView validation mechanism. For more information about this you can take a look at this blog post.

By the way can you provide me with some information about your requirements according to the GridViewComboBoxColumn? What else do you need from such kind of columns?

Regards,
Nedyalko Nikolov
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
GridView
Asked by
MiddleTommy
Top achievements
Rank 1
Answers by
MiddleTommy
Top achievements
Rank 1
Nedyalko Nikolov
Telerik team
Share this question
or