GridViewMaskedTextBoxColumn binding help

8 posts, 0 answers
  1. Pierre
    Pierre avatar
    212 posts
    Member since:
    Apr 2007

    Posted 17 Aug 2010 Link to this post

    Hi, I got a RadMaskedTexbox that change the mask and MaskType with binding. This RadMaskTexbox work well:

    <telerikInput:RadMaskedTextBox Grid.Row="5" Grid.Column="2" Name="txtCodePostal" Value="{Binding NouvAdresse.CodePostal, Mode=TwoWay, ValidatesOnExceptions=True}" Mask="{Binding PaysSelection.CodePostalMask, Mode=OneWay}" Width="200" HorizontalAlignment="Left" VerticalAlignment="Top" TabIndex="103" IsTabStop="True" MaskType="{Binding Path=CodePostalMask, Converter={StaticResource MaskTypeConverter}}"/>

    I try to do the same thing with a GridViewMaskedTextBoxColumn but I got some execption:

    Can't convert the object type 'System.Windows.Data.Binding' in type 'Telerik.Windows.Controls.MaskType'.
    Here my code:
    <telerikGridview:RadGridView Name="GridTelephone" Grid.Column="0" Grid.Row="1" CanUserFreezeColumns="False" CanUserReorderColumns="true" ShowGroupPanel="False" IsFilteringAllowed="false" 
                            AutoGenerateColumns="False" IsReadOnly="False" ItemsSource="{Binding NouvAdresse.Telephone, Mode=TwoWay}" 
                            HeaderRowStyle="{StaticResource GridViewHeaderRowStyle1}" Margin="3"
                            AddingNewDataItem="GridTelephone_AddingNewDataItem" RowEditEnded="GridTelephone_RowEditEnded">
        <telerikGridview:RadGridView.Columns>
            <telerikGridview:GridViewComboBoxColumn Header="Type" SelectedValueMemberPath="ID" DisplayMemberPath="Nom" IsFilterable="false" 
                                    DataMemberBinding="{Binding TypeTelephoneID}" ItemsSource="{Binding TypeTelephone, Mode=OneWay}"/>
            <telerikGridview:GridViewMaskedTextBoxColumn Header="Numéro" DataMemberBinding="{Binding Numero}" IsVisible="True" UniqueName="Numero" MaskType="{Binding Path=TelephoneMask, Converter={StaticResource MaskTypeConverter}}"></telerikGridview:GridViewMaskedTextBoxColumn>
            <telerikGridview:GridViewDataColumn Header="Ext" DataMemberBinding="{Binding Extention}" IsVisible="True" UniqueName="Extention" />
            <telerikGridview:GridViewDataColumn Header="Note" DataMemberBinding="{Binding Notes}" Width="*" IsVisible="True" UniqueName="Notes" />
        </telerikGridview:RadGridView.Columns>
    </telerikGridview:RadGridView>


    Any suggestion? Thank
  2. Nedyalko Nikolov
    Admin
    Nedyalko Nikolov avatar
    871 posts

    Posted 18 Aug 2010 Link to this post

    Hi Pierre,

    Indeed GridViewMaskedTextBoxColumn.Mask property is a string property, so you cannot set a binding to it.
    However you can easily workaround this with a custom column class:
    public class CustomMaskedTextBoxColumn : GridViewBoundColumnBase
        {
            private string mask = "aaaa";
            private MaskType? maskType;
     
            /// <summary>
            /// Gets or sets the type of the mask. This is a dependency property.
            /// </summary>
            /// <value>The type of the mask.</value>
            public MaskType MaskType
            {
                get
                {
                    return this.maskType.HasValue ? this.maskType.Value : MaskType.None;
                }
                set
                {
                    if (!this.maskType.HasValue || this.maskType.Value != value)
                    {
                        this.maskType = value;
                        this.OnPropertyChanged("MaskType");
                    }
                }
            }
     
            public Binding MaskTypeBinding
            {
                get;
                set;
            }
     
     
            public Binding MaskBinding
            {
                get;
                set;
            }
     
            /// <summary>
            /// Gets or sets the mask. This is a dependency property.
            /// </summary>
            /// <value>The mask.</value>
            public string Mask
            {
                get
                {
                    return this.mask;
                }
                set
                {
                    if (this.mask != value)
                    {
                        this.mask = value;
                        this.OnPropertyChanged("Mask");
                    }
                }
            }
     
            /// <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 cellEditElement = base.CreateCellEditElement(cell, dataItem);
     
                if (cellEditElement == null)
                {
                    if (this.DataMemberBinding != null && this.DataMemberBinding.Path != null)
                    {
                        var valueBinding = this.CreateValueBinding();
                        RadMaskedTextBox maskedTextBox = new RadMaskedTextBox();
     
                        if (this.MaskBinding != null)
                        {
                            maskedTextBox.SetBinding(RadMaskedTextBox.MaskProperty, this.MaskBinding);
                        }
                        else
                        {
                            maskedTextBox.Mask = this.Mask;
                        }
     
                        if (this.MaskTypeBinding != null)
                        {
                            maskedTextBox.SetBinding(RadMaskedTextBox.MaskTypeProperty, this.MaskTypeBinding);
                        }
                        else
                        {
                            maskedTextBox.MaskType = this.MaskType;
                        }
                         
                        maskedTextBox.UpdateValueEvent = UpdateValueEvent.PropertyChanged;
     
                        this.BindingTarget = RadMaskedTextBox.ValueProperty;
                        maskedTextBox.SetBinding(RadMaskedTextBox.ValueProperty, valueBinding);
     
                        cellEditElement = maskedTextBox;
                    }
                }
                return cellEditElement;
            }
     
            private Binding CreateValueBinding()
            {
                Binding valueBinding = new Binding();
     
                valueBinding.Path = new PropertyPath(this.DataMemberBinding.Path.Path);
                valueBinding.Mode = BindingMode.TwoWay;
                valueBinding.NotifyOnValidationError = true;
                valueBinding.ValidatesOnExceptions = true;
                valueBinding.ValidatesOnDataErrors = true;
                valueBinding.ValidatesOnNotifyDataErrors = true;
                valueBinding.UpdateSourceTrigger = UpdateSourceTrigger.Explicit;
                valueBinding.Source = this.DataMemberBinding.Source;
     
                return valueBinding;
            }
        }

    and the usage:

    <local:CustomMaskedTextBoxColumn Header="Age" DataMemberBinding="{Binding Age}" MaskType="Numeric" MaskBinding="{Binding Age, Converter={StaticResource maskConverter}}" />

    This class pretty much is a copy of the default GridViewMaskedTextBoxColumn except two new properties MaskTypeBinding and MaskBinding.

    Let me know how it goes.

    Best wishes,
    Nedyalko Nikolov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. DevCraft banner
  4. Pierre
    Pierre avatar
    212 posts
    Member since:
    Apr 2007

    Posted 18 Aug 2010 Link to this post

    Thank for your help.

    I add your custom class in my project.

    But after defining my column, the mask never work (the value appear without any mask). If I put a breakpoint i can see that the RadMaskedTexbox still at MaskType=None and Mask="aaaa".
    <Helper:CustomMaskedTextBoxColumn Header="Numéro" DataMemberBinding="{Binding Numero}" IsVisible="True" UniqueName="Numero" MaskBinding="{Binding PaysSelection.TelephoneMask, Mode=OneWay}" MaskTypeBinding="{Binding MaskTelephone, Mode=OneWay}"></Helper:CustomMaskedTextBoxColumn>
  5. Pierre
    Pierre avatar
    212 posts
    Member since:
    Apr 2007

    Posted 18 Aug 2010 Link to this post

    By the way, I see that we can override the CreateCellElement in you custom class. How I can use it to format the "normal cell" (not the edited one) with the mask pass in binding?
  6. Nedyalko Nikolov
    Admin
    Nedyalko Nikolov avatar
    871 posts

    Posted 19 Aug 2010 Link to this post

    Hello Pierre,

    1. Of course you can override CreateCellElement and provide a TextBlock with a custom text which is a function of the current Mask. You could use GridViewDataColumn.DataFormatString property or Binding.StringFormat.

    2. The default mask for RadMaskedTextBox is "aaaa" or something like that. You can test with different masks and mask type and you will see that these bindings are applied.

    Let me know if there are any further issues.

    Kind regards,
    Nedyalko Nikolov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  7. Pierre
    Pierre avatar
    212 posts
    Member since:
    Apr 2007

    Posted 20 Aug 2010 Link to this post

    I can't make this working...

    I call you customcolumn like this:
    <Helper:CustomMaskedTextBoxColumn Header="Numéro" Width="100" DataMemberBinding="{Binding Numero}" IsVisible="True" UniqueName="Numero" MaskBinding="{Binding PaysSelection.TelephoneMask, Mode=OneWay}" MaskTypeBinding="{Binding MaskTelephone, Mode=OneWay}"></Helper:CustomMaskedTextBoxColumn>

    I can see that the property MaskBinding and MaskTypeBinding change from null to a System.windows.data.binding (but I can't see the value) with this code:
    Binding m_MaskTypeBinding;
    public Binding MaskTypeBinding 
        get {return m_MaskTypeBinding;}
        set
        {
            m_MaskTypeBinding = MaskBinding;
        }
    }

    I add this event in your CreateCellEditElement to see property of the internal RadMaskedTextbox:
    maskedTextBox.ValueChanged += new EventHandler<Telerik.Windows.RadRoutedEventArgs>(maskedTextBox_ValueChanged);

    But the mask are not apply on the RadMaskedTextBox. In the MaskedTextBox_ValueChanged, i can see the Mask=aaaa and MaskType=None, like the default behavoiur the this control...

  8. Nedyalko Nikolov
    Admin
    Nedyalko Nikolov avatar
    871 posts

    Posted 23 Aug 2010 Link to this post

    Hello Pierre,

    Could you please send me your test application, because I cannot figure out what is going on?
    Thank you in advance.

    Best wishes,
    Nedyalko Nikolov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  9. Pierre
    Pierre avatar
    212 posts
    Member since:
    Apr 2007

    Posted 23 Aug 2010 Link to this post

    I can't add rar attachement here. I will open a support ticket.
Back to Top
DevCraft banner