DataFormComboBoxField Binding

13 posts, 0 answers
  1. Andrew
    Andrew avatar
    171 posts
    Member since:
    Jun 2009

    Posted 02 Aug 2011 Link to this post

    Within my Dataform i have a dropdown list which i bind to a IEnurable property on my ViewModel.
    This woks just fine as untill now all i wanted was to get the ID of the item selected.

    <telerik:DataFormComboBoxField DataMemberBinding="{Binding OwlRecordTypeId,Mode=TwoWay}" DisplayMemberPath="Name" SelectedValuePath="Id" ItemsSource="{Binding Source={StaticResource OWL_VM}, Path=OwlRecordTypes}" Label="Record Type:"/>

    However it turnes out that now i need some more info from the Entity.
    The entity has 3 fields ID, Code and Name.

    As you can see above i put the name in the DisplayMemberPath and the id into the selectedvaluepath

    To be able to see my "code" (the 3rd field) i would like to put teh whole object into the SelectedValuePath

    I tried this:

    SelectedValuePath="{Binding}"

    But it did not work.

    Any ideas on how i can do this?

  2. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 03 Aug 2011 Link to this post

    Hello Andrew,

    Basically, the SelectedValuePath property should be set, not bound. Please take a look at our online documentation for further reference.
    Let me know if you need any further assistance.

     

    Best wishes,
    Maya
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

  3. DevCraft banner
  4. Andrew
    Andrew avatar
    171 posts
    Member since:
    Jun 2009

    Posted 03 Aug 2011 Link to this post

    I have read the documentation and it does not anwer my question.
    This is exactly how i use it now.

    As i explained above what i am trying to do it instead of setting the SelectedValuePath to an ID, i would like to set it to the whole object, is that possible? and if so how would you do it?
  5. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 05 Aug 2011 Link to this post

    Hi Andrew,

    You may try to customize the data field by defining a RadComboBox inside and work with its SelectedItem and SelectedValue properties. This article illustrates a similar approach.
     

    Greetings,
    Maya
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

  6. Andrew
    Andrew avatar
    171 posts
    Member since:
    Jun 2009

    Posted 05 Aug 2011 Link to this post

    This article illustrates how to customise the data fields.
    But does not answer my question.

    Again, i am looking for a way to pass the whole entity rather than an ID.

    Has anyone else trying doing this?

  7. Ivan Ivanov
    Admin
    Ivan Ivanov avatar
    1128 posts

    Posted 08 Aug 2011 Link to this post

    Hello Andrew,

    I have prepared an example project you, trying to reproduce the scenario you have described. Please, refer to it and let me know whether this approach meets your requirements.

    All the best,
    Ivan Ivanov
    the Telerik team
    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>
  8. Andrew
    Andrew avatar
    171 posts
    Member since:
    Jun 2009

    Posted 09 Aug 2011 Link to this post

    I had a look at your example, but since it does not actually do anything, i was unsure what it is you are showing me.
    All i can see is that you do not use the DisplayMemberPath and the SelectedValuePath

    So i changed my code as follows:
    <telerik:DataFormComboBoxField DataMemberBinding="{Binding OwlRecordType,Mode=TwoWay}" ItemsSource="{Binding Source={StaticResource OWL_VM}, Path=OwlRecordTypes}" Label="Record Type:"/>

    This displayed the items in my drop down box but as numbered objects.
    I put back the DisplayMemberPath like this:
    <telerik:DataFormComboBoxField DataMemberBinding="{Binding OwlRecordType,Mode=TwoWay}" DisplayMemberPath="Name" ItemsSource="{Binding Source={StaticResource OWL_VM}, Path=OwlRecordTypes}" Label="Record Type:"/>

    This does load the items correctly, but does not save them.
    The item is added to the list but my "_SubmittedChanges(object sender,..."  does not run.
    It's very strange.
    I put a breakpoint there but nothing happens.
    I change the code back to my original one and it all works again.
  9. Pavel Pavlov
    Admin
    Pavel Pavlov avatar
    2039 posts

    Posted 09 Aug 2011 Link to this post

    Hello Andrew,

    To avoid further misunderstandings, can you please paste the implementation of your data objects and a short description of the expected behavior. This will definitely help us adjust our example to your requirements.

    Best wishes,
    Pavel Pavlov
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

  10. Andrew
    Andrew avatar
    171 posts
    Member since:
    Jun 2009

    Posted 11 Aug 2011 Link to this post

    Here is my metadata from the ria domain service generated file:

    // The MetadataTypeAttribute identifies OwlRecordMetadata as the class
        // that carries additional metadata for the OwlRecord class.
        [MetadataTypeAttribute(typeof(OwlRecord.OwlRecordMetadata))]
        public partial class OwlRecord
        {
      
            // This class allows you to attach custom attributes to properties
            // of the OwlRecord class.
            //
            // For example, the following marks the Xyz property as a
            // required property and specifies the format for valid values:
            //    [Required]
            //    [RegularExpression("[A-Z][A-Za-z0-9]*")]
            //    [StringLength(32)]
            //    public string Xyz { get; set; }
            internal sealed class OwlRecordMetadata
            {
      
                // Metadata classes are not meant to be instantiated.
                private OwlRecordMetadata()
                {
                }
      
                public string Description { get; set; }
      
                public int Id { get; set; }
      
                public string Notes { get; set; }
      
                public Organisation Organisation { get; set; }
      
                public int OrganisationId { get; set; }
      
                public OwlBillingType OwlBillingType { get; set; }
      
                public int OwlBillingTypeId { get; set; }
      
                public OwlRecordType OwlRecordType { get; set; }
      
                public int OwlRecordTypeId { get; set; }
      
                public OwlStateType OwlStateType { get; set; }
      
                public int OwlStateTypeId { get; set; }
      
                public OwlUnitType OwlUnitType { get; set; }
      
                public int OwlUnitTypeId { get; set; }
      
                public string Price { get; set; }
      
                public Nullable<short> TimeUnits { get; set; }
      
                public DateTime WorkDate { get; set; }
            }
        }
      
        // The MetadataTypeAttribute identifies OwlRecordTypeMetadata as the class
        // that carries additional metadata for the OwlRecordType class.
        [MetadataTypeAttribute(typeof(OwlRecordType.OwlRecordTypeMetadata))]
        public partial class OwlRecordType
        {
      
            // This class allows you to attach custom attributes to properties
            // of the OwlRecordType class.
            //
            // For example, the following marks the Xyz property as a
            // required property and specifies the format for valid values:
            //    [Required]
            //    [RegularExpression("[A-Z][A-Za-z0-9]*")]
            //    [StringLength(32)]
            //    public string Xyz { get; set; }
            internal sealed class OwlRecordTypeMetadata
            {
      
                // Metadata classes are not meant to be instantiated.
                private OwlRecordTypeMetadata()
                {
                }
      
                public string Code { get; set; }
      
                public Group Group { get; set; }
      
                public int GroupId { get; set; }
      
                public int Id { get; set; }
      
                public string Name { get; set; }
      
                [Include]
                public EntityCollection<OwlRecord> OwlRecords { get; set; }
            }
        }

    This is only the relevant part of course.

    I then have this im my viewmodel:
    private QueryableDomainServiceCollectionView<OwlRecord> _owlRecords;
      
            public IEnumerable OwlRecords
            {
                get { return _owlRecords; }
            }
      
    public IEnumerable OwlBillingTypes
            {
                get { return _owlBillingTypes; }
            }
      
            private QueryableDomainServiceCollectionView<OwlRecordType> _owlRecordTypes;

    Right now i bind the owlRecordTypes to my drop down like this:
    <telerik:DataFormComboBoxField DataMemberBinding="{Binding OwlRecordTypeId,Mode=TwoWay}" DisplayMemberPath="Name" SelectedValuePath="Id" ItemsSource="{Binding Source={StaticResource OWL_VM}, Path=OwlRecordTypes}" Label="Record Type:"/>

    This is inside a grid bouind to a newRecord. This way inside the command that is bound to a button i do this:
    OwlRecord myRecord = new OwlRecord();
      
                myRecord.OwlStateTypeId = 1;
                myRecord.OwlRecordTypeId = _newOwlRecord.OwlRecordTypeId;
                                                     
                _owlRecordsCurrent.AddNew(myRecord);
                _owlRecordsCurrent.SubmitChanges();

    But would like to be able to say:
    if( _newOwlRecord.OwlRecordType.Code = "this")
       myRecord.OwlRecordType = _newOwlRecord.OwlRecordType;
    else
       something else
    to do this i need to bind it something like this:
    <telerik:DataFormComboBoxField DataMemberBinding="{Binding OwlRecordType,Mode=TwoWay}" DisplayMemberPath="Name" SelectedValuePath="???????" ItemsSource="{Binding Source={StaticResource OWL_VM}, Path=OwlRecordTypes}" Label="Record Type:"/>
    Question is what do i put instead of the ????? or do i take out that part completely. Or how do i do it otherwise?
  11. Pavel Pavlov
    Admin
    Pavel Pavlov avatar
    2039 posts

    Posted 18 Aug 2011 Link to this post

    Hello Andrew,

    I am attaching a sample app with the suggested setup. It uses stripped versions of your data objects , with the relevant properties only.

    Kind regards,
    Pavel Pavlov
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

  12. Andrew
    Andrew avatar
    171 posts
    Member since:
    Jun 2009

    Posted 23 Aug 2011 Link to this post

    Your example does not actually do anything with the selection so it is untestable.
    Also, this is pretty much the same code i had on the 9th of august. Please see post above.

    I had it display the correct items in the dropdown. But doing it this way it made the project stop getting to the submitedchanges event.

    As described in post on the 9th of august.

  13. Pavel Pavlov
    Admin
    Pavel Pavlov avatar
    2039 posts

    Posted 25 Aug 2011 Link to this post

    Hello Andrew,

    I believe that debugging why your event is not fired would require me to have a look at your project.
    To be able to attach the project , please open a support ticket  refering this forum thread ( id 449848).

    As I am not sure the problem is related to RadDataForm at all,  I may also suggest to test it with the MS DataForm and let me know in case you get better results there.

    Best wishes,
    Pavel Pavlov
    the Telerik team

    Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

  14. Andrew
    Andrew avatar
    171 posts
    Member since:
    Jun 2009

    Posted 23 Mar 2012 Link to this post

    I can't provide you with my entire project as it is too large to post.
    Again, all the necessary code has been provided above.

    The code you have provided to me does NOT do anything. It is simply sample code. Sample that does not work in actual real live application.

    You are missing the SelectedValuePath from your code. Without it I cannot save anything. Did you leave it out on purpose? And if so how do I go about passing this information of the selected item?

Back to Top
DevCraft banner