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

DataFormComboBoxField Binding

12 Answers 242 Views
DataForm
This is a migrated thread and some comments may be shown as answers.
Andrew
Top achievements
Rank 1
Andrew asked on 02 Aug 2011, 03:57 PM

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?

12 Answers, 1 is accepted

Sort by
0
Maya
Telerik team
answered on 03 Aug 2011, 07:49 AM
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 >>

0
Andrew
Top achievements
Rank 1
answered on 03 Aug 2011, 10:45 AM
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?
0
Maya
Telerik team
answered on 05 Aug 2011, 10:36 AM
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 >>

0
Andrew
Top achievements
Rank 1
answered on 05 Aug 2011, 01:54 PM

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?

0
Ivan Ivanov
Telerik team
answered on 08 Aug 2011, 03:39 PM
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 >>
0
Andrew
Top achievements
Rank 1
answered on 09 Aug 2011, 01:15 PM
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.
0
Pavel Pavlov
Telerik team
answered on 09 Aug 2011, 02:30 PM
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 >>

0
Andrew
Top achievements
Rank 1
answered on 11 Aug 2011, 03:38 PM

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?
0
Pavel Pavlov
Telerik team
answered on 18 Aug 2011, 12:38 PM
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 >>

0
Andrew
Top achievements
Rank 1
answered on 23 Aug 2011, 03:15 PM

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.

0
Pavel Pavlov
Telerik team
answered on 25 Aug 2011, 04:48 PM
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 >>

0
Andrew
Top achievements
Rank 1
answered on 23 Mar 2012, 10:30 AM

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?

Tags
DataForm
Asked by
Andrew
Top achievements
Rank 1
Answers by
Maya
Telerik team
Andrew
Top achievements
Rank 1
Ivan Ivanov
Telerik team
Pavel Pavlov
Telerik team
Share this question
or