Binding Fields in DataForm to more then one object

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

    Posted 19 Jun 2011 Link to this post

    I would like to bind some fields in my DataForm two one object from my VewModel and some others ones to another object.
    Maybe a better way of explaning is, i would like to show two seperate objects in the same dataform.

    I was hoping to do something like this:

    <telerik:RadDataForm x:Name="RadClientDetails" CurrentItem="{Binding SelectedOrganisation}" AutoGenerateFields="False">
                            <telerik:RadDataForm.ReadOnlyTemplate>
                                <DataTemplate>
                                    <StackPanel>
                                        <StackPanel>
                                            <telerik:DataFormDataField DataMemberBinding="{Binding A_Name,Mode=OneTime}" Label="Organisation Name:" IsEnabled="False" />
                                            <telerik:DataFormDataField DataMemberBinding="{Binding A_Description,Mode=OneTime}" Label="Description:" IsEnabled="False"/>
                                            <telerik:DataFormDataField DataMemberBinding="{Binding WebSite,Mode=OneTime}" Label="Web Site:" IsEnabled="False"/>
                                            <telerik:DataFormDataField DataMemberBinding="{Binding A_Notes,Mode=OneTime}" Label="Notes:" IsEnabled="False" />
                                        </StackPanel>
                                        <StackPanel DataContext="{Binding PrimaryAddress}">
                                            <telerik:DataFormDataField DataMemberBinding="{Binding MainText,Mode=OneTime}" Label="Address:" IsEnabled="False" />
                                            <telerik:DataFormDataField DataMemberBinding="{Binding City,Mode=OneTime}" Label="City:" IsEnabled="False"/>
                                            <telerik:DataFormDataField DataMemberBinding="{Binding County,Mode=OneTime}" Label="County:" IsEnabled="False"/>
                                            <telerik:DataFormDataField DataMemberBinding="{Binding PostCode,Mode=OneTime}" Label="Post Code:" IsEnabled="False"/>
                                            <telerik:DataFormDataField DataMemberBinding="{Binding Country,Mode=OneTime}" Label="Country:" IsEnabled="False"/>
                                        </StackPanel>


    But it is not working. I guess the SelectedOrganisation does not have the PrimaryAddress object within itself.

    Any idea how i could do this?
    I want to let the user edit all the fields in one go, and not have to click the edit button seperately in each dataform.
  2. Pavel Pavlov
    Admin
    Pavel Pavlov avatar
    2039 posts

    Posted 20 Jun 2011 Link to this post

    Hello Andrew,

    Indeed , only a single object may be edited by RadDataForm at a time.

    As a solution I would recommend either a proxy object or a view model , exposing all the properties to the RadDataForm and dispatching changes to the 2 objects when a property is changed.

    Let me know in case you need  a sample on that.

    Greetings,
    Pavel Pavlov
    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. Andrew
    Andrew avatar
    171 posts
    Member since:
    Jun 2009

    Posted 20 Jun 2011 Link to this post

    An example would be great. Thank you.
    I am using a VewModel now.
    Bellow is my code it might make it clear what i am trying to do:

    private QueryableDomainServiceCollectionView<Organisation> _organisations;
              
            public IEnumerable Organisations
            {
                get { return _organisations; }
            }
      
            private Organisation _selectedOrganisation;
      
            public Organisation SelectedOrganisation
            {
                get { return _selectedOrganisation; }
                set
                {
                    if (value != _selectedOrganisation)
                    {
                        _selectedOrganisation = value;
                        _primaryAddress = _selectedOrganisation.Offices.First(e => e.Z_Position == 0).Addresses.First(e => e.Z_Position == 0);
                        _primaryPhoneNumber = _selectedOrganisation.Offices.First(e => e.Z_Position == 0).CommsMethods.First(e => e.Z_Position == 0);
                        _primaryFaxNumber = _selectedOrganisation.Offices.First(e => e.Z_Position == 0).CommsMethods.First(e => e.Z_Position == 1);
                        OnPropertyChanged("SelectedOrganisation");
                        OnPropertyChanged("PrimaryAddress");
                        OnPropertyChanged("PrimaryPhoneNumber");
                        OnPropertyChanged("PrimaryFaxNumber");
                    }
                }
            }
      
            private Address _primaryAddress;
      
            public Address PrimaryAddress
            {
                get
                { return _primaryAddress; }
                set
                {
                    if (value != _primaryAddress)
                    {
                        _primaryAddress = value;
                        OnPropertyChanged("PrimaryAddress");
                    }
                }
            }
      
            private CommsMethod _primaryPhoneNumber;
      
            public CommsMethod PrimaryPhoneNumber
            {
                get
                { return _primaryPhoneNumber; }
                set
                {
                    if (value != _primaryPhoneNumber)
                    {
                        _primaryPhoneNumber = value;
                        OnPropertyChanged("PrimaryPhoneNumber");
                    }
                }
            }
      
            private CommsMethod _primaryFaxNumber;
      
            public CommsMethod PrimaryFaxNumber
            {
                get
                { return _primaryFaxNumber; }
                set
                {
                    if (value != _primaryFaxNumber)
                    {
                        _primaryFaxNumber = value;
                        OnPropertyChanged("PrimaryFaxNumber");
                    }
                }
            }

    <telerik:RadGridView x:Name="RadClientList" AutoGenerateColumns="False" ItemsSource="{Binding Path=Organisations}" SelectedItem="{Binding SelectedOrganisation, Mode=TwoWay}" IsReadOnly="True">

    <telerik:RadDataForm x:Name="RadClientDetails" CurrentItem="{Binding SelectedOrganisation}" AutoGenerateFields="False" BeginningEdit="RadClientDetails_BeginningEdit" CommandButtonsVisibility="Edit" ValidationSummaryVisibility="Collapsed" BorderThickness="1,1,1,0">
  5. Andrew
    Andrew avatar
    171 posts
    Member since:
    Jun 2009

    Posted 20 Jun 2011 Link to this post

    Hi Pavel,

    I have tried to implement your idea.
    I have now an object in my viewmodel that contains the other objects that i need on my dataform.

    It all works fairly well, only problem is that my cancel button never enables.
    The ok (commit) button works just fine. I can edit it all.
    I think it might be because my new class (object) is a simple object and does not inherit from anything. I guess it needs to be some editible object or something. Am i correct?
    Can you help with that.

    Bellow is my new object and ViewModel:

    public class OrganisationDetails
        {
            public Organisation currentOrganisation { get; set; }
            public Address primaryAddress { get; set; }
            public CommsMethod primaryPhoneNumber { get; set; }
            public CommsMethod primaryFaxNumber { get; set; }
      
        }
    public class OrganisationsManagerViewModel : ViewModelBase
        {
            RabbitDomainContext _context = new RabbitDomainContext();
      
            public ICommand SaveChangesCommand { get; set; }
      
            public OrganisationsManagerViewModel()
            {
                if (!DesignerProperties.IsInDesignTool)
                {
                    EntityQuery<Organisation> getOrganisationQuery = _context.GetOrganisationsQuery();
                    _organisations = new QueryableDomainServiceCollectionView<Organisation>(_context, getOrganisationQuery);
                    _organisations.PageSize = 25;
                    _organisations.AutoLoad = true;
      
                    EntityQuery<VisibilityType> getVisibiltyTypesQuert = _context.GetVisibilityTypesQuery();
                    _visibilityTypes = new QueryableDomainServiceCollectionView<VisibilityType>(_context, getVisibiltyTypesQuert);
                    _visibilityTypes.PageSize = 3;
                    _visibilityTypes.AutoLoad = true;
      
                    EntityQuery<Country> getCountriesQuert = _context.GetCountriesQuery();
                    _countries = new QueryableDomainServiceCollectionView<Country>(_context, getCountriesQuert);
                    _countries.PageSize = 25;
                    _countries.AutoLoad = true;
      
                    SaveChangesCommand = new DelegateCommand(SaveChanges, CanSaveChanges);
                }
            }
      
            private QueryableDomainServiceCollectionView<Organisation> _organisations;
              
            public IEnumerable Organisations
            {
                get { return _organisations; }
            }
      
            private QueryableDomainServiceCollectionView<VisibilityType> _visibilityTypes;
      
            public IEnumerable VisibilityTypes
            {
                get { return _visibilityTypes; }
            }
      
            private QueryableDomainServiceCollectionView<Country> _countries;
      
            public IEnumerable Countries
            {
                get { return _countries; }
            }
      
            private Organisation _selectedOrganisation;
      
            public Organisation SelectedOrganisation
            {
                get { return _selectedOrganisation; }
                set
                {
                    if (value != _selectedOrganisation)
                    {
                        _selectedOrganisation = value;
                        _primaryAddress = _selectedOrganisation.Offices.First(e => e.Z_Position == 0).Addresses.First(e => e.Z_Position == 0);
                        _primaryPhoneNumber = _selectedOrganisation.Offices.First(e => e.Z_Position == 0).CommsMethods.First(e => e.Z_Position == 0);
                        _primaryFaxNumber = _selectedOrganisation.Offices.First(e => e.Z_Position == 0).CommsMethods.First(e => e.Z_Position == 1);
                        OnPropertyChanged("SelectedOrganisation");
                        OnPropertyChanged("PrimaryAddress");
                        OnPropertyChanged("PrimaryPhoneNumber");
                        OnPropertyChanged("PrimaryFaxNumber");
      
                        _selectedOrganisationDetails = new OrganisationDetails();
                        _selectedOrganisationDetails.currentOrganisation = SelectedOrganisation;
                        _selectedOrganisationDetails.primaryAddress = _primaryAddress;
                        _selectedOrganisationDetails.primaryPhoneNumber = _primaryPhoneNumber;
                        _selectedOrganisationDetails.primaryFaxNumber = _primaryFaxNumber;
                        OnPropertyChanged("SelectedOrganisationDetails");
      
                    }
                }
            }
      
            private OrganisationDetails _selectedOrganisationDetails;
      
            public OrganisationDetails SelectedOrganisationDetails
            {
                get { return _selectedOrganisationDetails; }
                set
                {
                    if (value != _selectedOrganisationDetails)
                    {
                        OnPropertyChanged("SelectedOrganisationDetails");
                    }
                }
            }
      
            private Address _primaryAddress;
      
            public Address PrimaryAddress
            {
                get
                { return _primaryAddress; }
                set
                {
                    if (value != _primaryAddress)
                    {
                        _primaryAddress = value;
                        OnPropertyChanged("PrimaryAddress");
                    }
                }
            }
      
            private CommsMethod _primaryPhoneNumber;
      
            public CommsMethod PrimaryPhoneNumber
            {
                get
                { return _primaryPhoneNumber; }
                set
                {
                    if (value != _primaryPhoneNumber)
                    {
                        _primaryPhoneNumber = value;
                        OnPropertyChanged("PrimaryPhoneNumber");
                    }
                }
            }
      
            private CommsMethod _primaryFaxNumber;
      
            public CommsMethod PrimaryFaxNumber
            {
                get
                { return _primaryFaxNumber; }
                set
                {
                    if (value != _primaryFaxNumber)
                    {
                        _primaryFaxNumber = value;
                        OnPropertyChanged("PrimaryFaxNumber");
                    }
                }
            }
      
            public bool IsBusy
            {
                get
                {
                    return false; //this._organisations.IsBusy;
                }
            }
      
            #region Command Stuff
      
            private bool CanSaveChanges(object param)
            {           
                return true;  
            }
      
            private void SaveChanges(object param)
            {
                _organisations.SubmitChanges();
            }
      
            #endregion
        }
  6. Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 21 Jun 2011 Link to this post

    Hello,

     Indeed you need IEditableObject to achieve this. 

    Regards,
    Vlad
    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. Andrew
    Andrew avatar
    171 posts
    Member since:
    Jun 2009

    Posted 21 Jun 2011 Link to this post

    Any chance of an example on how i would do that?
    Thanks
  8. Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 21 Jun 2011 Link to this post

    Hi,

     This is just an interface - you need to implement it for your data objects. 

    All the best,
    Vlad
    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. Andrew
    Andrew avatar
    171 posts
    Member since:
    Jun 2009

    Posted 21 Jun 2011 Link to this post

    Any chance of a sample?
  10. Andrew
    Andrew avatar
    171 posts
    Member since:
    Jun 2009

    Posted 28 Jun 2011 Link to this post

    So no news on that example Pavel?

    I just don't understand how to implement that.

    The new iEditableobject contains the 3 Entities i need to edit.

    public class OrganisationDetails : IEditableObject
        {
            public Organisation currentOrganisation { get; set; }
            public Address primaryAddress { get; set; }
            public CommsMethod primaryPhoneNumber { get; set; }
      
    #region IEditableObject Members
      
            public void BeginEdit()
            {
                //
            }
      
            public void CancelEdit()
            {
                  
            }
      
            public void EndEdit()
            {
                //
            }
      
            #endregion
        }

    But i don't know how to implemement the CancelEdit event.

    The entity objexts do not have a CancelEdit method or anything similar.

    How do i roll back any changes from thos objects inside my new CancelEdit event?
  11. Andrew
    Andrew avatar
    171 posts
    Member since:
    Jun 2009

    Posted 05 Jul 2011 Link to this post

    With advice from silverlight.net forums. I have implemented the CancelEdit method like this:

    public void CancelEdit()
            {
                (currentOrganisation as IEditableObject).CancelEdit();
                (primaryAddress as IEditableObject).CancelEdit();
            }

     

    But when I press the cancel button the new data is not cleared.

    Any help on this?

  12. Pavel Pavlov
    Admin
    Pavel Pavlov avatar
    2039 posts

    Posted 06 Jul 2011 Link to this post

    Hi Andrew,

    I have noticed you have implemented the interface , but your methods are empty.

    e.g. :
    public void BeginEdit()
     
            {
     
                //
     
            }
     
       
      
            public void CancelEdit()
     
            {
     
                   
      
            }
     
       
      
            public void EndEdit()
     
            {
     
                //
     
            }




    You may find  a sample implementation of the IEditableObject interface  here.

    Kind regards,
    Pavel Pavlov
    the Telerik team

    Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

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

    Posted 12 Jul 2011 Link to this post

    I see, using the link i tried to implement the interface.
    Since this are Entity objects i of course cannot simply assign a current object to a backup object, if i do that any changes in the current object will show on the backup too.
    Therefore i had to assign each property value separately.
    That does work, however it does raise the question of using the DataForm. This implementation makes it rather pointless; it’s not much different from actually creating one using a bunch of textboxes.

Back to Top
DevCraft banner