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

Binding between UserControl (with rad Controls) and radGridView

3 Answers 63 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
ENTERPRISE INTERNATIONAL SAS
Top achievements
Rank 1
ENTERPRISE INTERNATIONAL SAS asked on 29 Oct 2010, 11:11 PM
hi!

I developed a UserControl with a RadComboBox and RadMaskedTextBox, snippet code is like this:

<cl:ctlIdentificacionPersona
    xmlns:cl="clr-namespace:App_ControlLibrary"
    xmlns:tb="clr-namespace:App_EntityLibrary.Tablas;assembly=App_EntityLibrary"
    mc:Ignorable="d"
    x:Name="UserControl" Height="62" Width="368">
    <Grid x:Name="pnlBaseControl" RenderTransformOrigin="0.5,0.5" Margin="0,0,5,0" Height="59" VerticalAlignment="Top">
        <TextBlock x:Name="lblTipoDocumento" Text="{x:Static cl:resxIdentificacionPersona.lblTipoDocumento}" VerticalAlignment="Top" Height="21" Margin="5,4,170,0" Padding="68,2,0,5"/>
        <telerik:RadComboBox x:Name="cbxTipoDocumento" SelectedValuePath="CA001" DisplayMemberPath="CA002" IsEditable="False" HorizontalAlignment="Left" Margin="5,26,0,0" VerticalAlignment="Top" Height="27" Width="190" TabIndex="0"/>
        <TextBlock x:Name="lblNumeroDocumento" Text="{x:Static cl:resxIdentificacionPersona.lblNumeroDocumento}" Margin="199,4,0,0" VerticalAlignment="Top" Height="21" Padding="41,2,0,5" HorizontalAlignment="Left" Width="118"/>
        <telerik:RadMaskedTextBox x:Name="fmtNumeroDocumento" Margin="197,26,0,0" TabIndex="1" Mask="n0" MaskType="Numeric" HorizontalContentAlignment="Right" Height="27" VerticalAlignment="Top" HorizontalAlignment="Left" Width="120"/>
        <telerik:RadButton x:Name="btnBuscar" Margin="321,16,0,2" HorizontalAlignment="Left" Width="39" Background="{x:Null}" BorderBrush="{x:Null}">
            <Image Source="/App_ControlLibrary;component/Imagenes/Lupa.png" Stretch="Uniform" Height="35" Width="34" />
        </telerik:RadButton>
    </Grid>
</cl:ctlIdentificacionPersona>

into my main project I declared UserControl like this:

<Grid>
<cl:ctlIdentificacionPersona x:Name="ctrDatosPersona" Tag="TABLA_PRINCIPAL" Height="Auto" Margin="1,3.02,5,1.73" Width="Auto">
<cl:ctlIdentificacionPersona.ValorTipoDocumento>
   <Binding ElementName="grdGridConsulta" Path="SelectedItem.CA003" Mode="TwoWay" UpdateSourceTrigger="Explicit" Converter="{StaticResource ConvertidorGeneralValores}">
      <Binding.ValidationRules>
        <cl:ReglaDatoRequerido MensajeError="{lr:RecursoIdioma IDRecurso=msjErrorDatoRequeridoTipoDocumento, TipoRecursoIdioma=PADRE}" ValidationStep="ConvertedProposedValue"/>
      </Binding.ValidationRules>
   </Binding>                                      
</
cl:ctlIdentificacionPersona.ValorTipoDocumento>
<cl:ctlIdentificacionPersona.ValorNumeroDocumento>
   <Binding ElementName="grdGridConsulta" Path="SelectedItem.CA004" Mode="TwoWay" UpdateSourceTrigger="Explicit" Converter="{StaticResource ConvertidorGeneralValores}">
     <Binding.ValidationRules>
        <cl:ReglaDatoRequerido MensajeError="{lr:RecursoIdioma IDRecurso=msjErrorDatoRequeridoNumeroDocumento, TipoRecursoIdioma=PADRE}" ValidationStep="ConvertedProposedValue"/>
     </Binding.ValidationRules>
   </Binding>
</cl:ctlIdentificacionPersona.ValorNumeroDocumento>
</cl:ctlIdentificacionPersona>
</Grid>

I declared 2 dependency properties into My UserControl named ValorTipoDocumento and ValorNumeroDocumento in this way:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Configuration;
 
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.MaskedTextBox;
 
using Kernel.Recursos;
using App_EntityLibrary;
using App_EntityLibrary.Tablas;
using System.Event;
 
namespace App_ControlLibrary
{
    public class ctlIdentificacionPersona : UserControl
    {
        #region Variables
 
        private RadComboBox cbxTipoDocumento;
        private RadMaskedTextBox fmtNumeroDocumento;
        private RadButton btnBuscar;
 
        ....
          
        public static readonly DependencyProperty ValorNumeroDocumentoProperty = DependencyProperty.Register("ValorNumeroDocumento", typeof(object), typeof(ctlIdentificacionPersona), new FrameworkPropertyMetadata(new PropertyChangedCallback(ValorNumeroDocumentoPropertyChanged)));
 
        [Description("Valor contenido en el NĂºmero de Documento")]
        public object ValorNumeroDocumento
        {
            get { return GetValue(ValorNumeroDocumentoProperty); }
            set { SetValue(ValorNumeroDocumentoProperty, value); }
        }
 
        public static readonly DependencyProperty ValorTipoDocumentoProperty = DependencyProperty.Register("ValorTipoDocumento", typeof(object), typeof(ctlIdentificacionPersona), new FrameworkPropertyMetadata(new PropertyChangedCallback(ValorTipoDocumentoPropertyChanged)));
 
        [Description("Valor Seleccionado en la lista")]
        public object ValorTipoDocumento
        {
            get { return GetValue(ValorTipoDocumentoProperty); }
            set { SetValue(ValorTipoDocumentoProperty, value); }
        }
 
        ...
 
        public ctlIdentificacionPersona()
        {
            InitComponents();
            cbxTipoDocumento = (RadComboBox)this.FindName("cbxTipoDocumento");
            fmtNumeroDocumento = (RadMaskedTextBox)this.FindName("fmtNumeroDocumento");
            btnBuscar = (RadButton)this.FindName("btnBuscar");
        }
 
 
 
         private static void ValorNumeroDocumentoPropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
        {
            (source as ctlIdentificacionPersona).ActualizarValorNumeroDocumento(e.NewValue);
        }
 
private void ActualizarValorNumeroDocumento(object value)
        {
            fmtNumeroDocumento.Value = value;
        }
 
        private static void ValorTipoDocumentoPropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
        {
            (source as ctlIdentificacionPersona).ActualizarValorTipoDocumento(e.NewValue);
        }
 
private void ActualizarValorTipoDocumento(object value)
        {
            cbxTipoDocumento.SelectedValue = value;
        }
 
...
}

I hope so far It could have be understood!! I try to make clear at the most.

I bounded my UserControl to the radGridView by using the two properties I created to refelct changes in both way. 

Note: <Binding ElementName="grdGridConsulta" Path="SelectedItem.CA003" Mode="TwoWay" AND
<Binding ElementName="grdGridConsulta" Path="SelectedItem.CA004" Mode="TwoWay"

When I navigate into RadGridView (I mean, selectedItemChanged is fired), UserControl reflects the changes in right way (I mean radComboBox and radMaskedTextBox). PROBLEMS begin when I select other item into radComboBox and I change text into radMaskedTextBox, and I try to update source data in RadGridView, by using:

ctrDatosPersona.GetBindingExpression(ctlIdentificacionPersona.ValorTipoDocumentoProperty).UpdateSource();
ctrDatosPersona.GetBindingExpression(ctlIdentificacionPersona.ValorNumeroDocumentoProperty).UpdateSource();

Data are null. RadGridView shows empty data into corresponding cells. I try to do a trace and ctrDatosPersona doesn't return selected values and text changed, in fact, returns  {} for ValorTipoDocumento and nothing for ValorNumeroDocumento. The way I realized about this was by tracing ValidationRules values.

I don't know how to do to radGridView reflects changes from my UserControl.

It's urgent, any helps I appreciate



3 Answers, 1 is accepted

Sort by
0
Veselin Vasilev
Telerik team
answered on 04 Nov 2010, 10:36 AM
Hi Ramiro,

We have a similar blog post and a sample project here. Basically, to edit an item outside the gridview you need to call gridView.Items.EditItem(item) and gridView.Items.CommitEdit() methods if you want the updated values to be shown automatically in the gridview.

Hope this helps.

Kind regards,
Veselin Vasilev
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
0
ENTERPRISE INTERNATIONAL SAS
Top achievements
Rank 1
answered on 04 Nov 2010, 08:07 PM
Hi!


Thanks, problem is not in radGridView, problem seems to be in UserControl. This instructions:

ctrDatosPersona.GetBindingExpression(ctlIdentificacionPersona.ValorTipoDocumentoProperty).UpdateSource();
ctrDatosPersona.GetBindingExpression(ctlIdentificacionPersona.ValorNumeroDocumentoProperty).UpdateSource();

have this ValidationRules:

public class ReglaDatoRequerido : ReglaValidacionBase
    {
        public ReglaDatoRequerido()
        {
        }
 
        public ReglaDatoRequerido(string vsMensajeError)
        {
            MensajeError = vsMensajeError;
        }
 
        /// <summary>
        /// Valida si un valor es nulo o vacio
        /// </summary>
        /// <param name="value"></param>
        /// <param name="cultureInfo"></param>
        /// <returns></returns>
        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            if (value == null || String.IsNullOrEmpty(value.ToString()))
                return new ValidationResult(false, MensajeError);
 
            return ValidationResult.ValidResult;
        }
    }

and when validate is triggered value for ctlIdentificacionPersona.ValorTipoDocumentoProperty is {}, and for ctlIdentificacionPersona.ValorNumeroDocumentoProperty is null. 

I posted a support ticket about this and about the other similar one problem.
0
ENTERPRISE INTERNATIONAL SAS
Top achievements
Rank 1
answered on 06 Nov 2010, 05:11 PM
Hi

I found by myself solution to this scenario!!


Thanks for your help!

Tags
General Discussions
Asked by
ENTERPRISE INTERNATIONAL SAS
Top achievements
Rank 1
Answers by
Veselin Vasilev
Telerik team
ENTERPRISE INTERNATIONAL SAS
Top achievements
Rank 1
Share this question
or