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

Data Form With Custom DataTemplate

1 Answer 64 Views
DataForm
This is a migrated thread and some comments may be shown as answers.
David
Top achievements
Rank 2
David asked on 17 Mar 2012, 02:23 AM
Hi,

I have three classes Employee, EmployeeCollection and Address

I have set the datacontext to a EmployeeCollection this works ok until I get to the Address Field then i only get one textbox. I have tried to create a DataTemplate for Address but  it doesn't seem to use the template I have created. 

Thanks 

David

public class Address
    {
        String _Street;
        String _City;
        String _Town;
        String _County;
        String _PostCode;
 
        public String Street
        {
            get
            {
                return this._Street;
            }
            set
            {
                this._Street = value;
                NotifyPropertyChanged(() => Street);
            }
        }
        public String City
        {
            get
            {
                return this._City;
            }
            set
            {
                this._City = value;
                NotifyPropertyChanged(() => City);
            }
        }
        public String Town
        {
            get
            {
                return this._Town;
            }
            set
            {
                this._Town = value;
                NotifyPropertyChanged(() => Town);
            }
        }
        public String County
        {
            get
            {
                return this._County;
            }
            set
            {
                this._County = value;
                NotifyPropertyChanged(() => County);
            }
        }
        public String PostCode
        {
            get
            {
                return this._PostCode;
            }
            set
            {
                this._PostCode = value;
                NotifyPropertyChanged(() => PostCode);
            }
        }
 
        public Address()
        {
        }
 
        public Address(String street, String city, String town, String county, String postCode)
        {
            this.Street = street;
            this.City = city;
            this.Town = town;
            this.County = county;
            this.PostCode = postCode;
        }
 
        #region PropertyNotify
 
         
        [field: NonSerializedAttribute()]
        public event PropertyChangedEventHandler PropertyChanged = delegate { };
 
        public readonly Action<Action> _synchronousInvoker;
 
        public Address(Action<Action> synchronousInvoker)
        {
            _synchronousInvoker = synchronousInvoker;
        }
 
        public void NotifyPropertyChanged<T>(Expression<Func<T>> expr)
        {
            var body = expr.Body as MemberExpression;
            if (body != null)
            {
                try
                {
                    if (_synchronousInvoker != null)
                        _synchronousInvoker(() => InvokePropertyChanged(expr));
                    else
                        InvokePropertyChanged(expr);
                }
                catch (Exception Ex)
                {
                    if (Ex.Message == "Invoke or BeginInvoke cannot be called on a control until the window handle has been created.")
                    {
                        InvokePropertyChanged(expr);
                    }
                }
            }
        }
 
        private void InvokePropertyChanged<T>(Expression<Func<T>> expr)
        {
            var body = expr.Body as MemberExpression;
            if (body != null)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(body.Member.Name));
                }
            }
        }
 
        #endregion
    }
 
 public class Employee : INotifyPropertyChanged
    {
        Int32 _EmployeeID;
        String _FirstName;
        String _SurName;
        Location _DefaultStore;
        float _Sallary;
        float _HourlyRate;
        EmployeeType _EmployeeType;
        Address _Address;
 
        public Int32 EmployeeID
        {
            get
            {
                return _EmployeeID;
            }
            set
            {
                _EmployeeID = value;
                NotifyPropertyChanged(() => EmployeeID);
            }
        }
        public String FirstName
        {
            get
            {
                return this._FirstName;
            }
            set
            {
                this._FirstName = value;
                NotifyPropertyChanged(() => FirstName);
            }
        }
        public String SurName
        {
            get
            {
                return this._SurName;
            }
            set
            {
                this._SurName = value;
                NotifyPropertyChanged(() => SurName);
            }
        }
        public Location DefaultStore
        {
            get
            {
                return this._DefaultStore;
            }
            set
            {
                this._DefaultStore = value;
                NotifyPropertyChanged(() => DefaultStore);
            }
        }
        public float Sallary
        {
            get
            {
                return this._Sallary;
            }
            set
            {
                this._Sallary = value;
                NotifyPropertyChanged(() => Sallary);
            }
        }
        public float HourlyRate
        {
            get
            {
                return this._HourlyRate;
            }
            set
            {
                this._HourlyRate = value;
                NotifyPropertyChanged(() => HourlyRate);
            }
        }
        public EmployeeType EmployeeType
        {
            get
            {
                return this._EmployeeType;
            }
            set
            {
                this._EmployeeType = value;
                NotifyPropertyChanged(() => EmployeeType);
            }
        }
        public Address Address
        {
            get
            {
                return _Address;
            }
            set
            {
                _Address = value;
                NotifyPropertyChanged(() => Address);
            }
        }
 
        public Employee()
        {
        }
 
        public Employee(Int32 employeeID, String firstName, String surName, Location defaultStore, float sallary, float hourlyRate, EmployeeType employeeType, Address address)
        {
            this._EmployeeID = employeeID;
            this._FirstName = firstName;
            this._SurName = surName;
            this._DefaultStore = defaultStore;
            this._Sallary = sallary;
            this._HourlyRate = hourlyRate;
            this._EmployeeType = employeeType;
            this._Address = address;
        }
 
        #region PropertyNotify
 
        [field: NonSerializedAttribute()]
        public event PropertyChangedEventHandler PropertyChanged = delegate { };
 
        public readonly Action<Action> _synchronousInvoker;
 
        public Employee(Action<Action> synchronousInvoker)
        {
            _synchronousInvoker = synchronousInvoker;
        }
 
        public void NotifyPropertyChanged<T>(Expression<Func<T>> expr)
        {
            var body = expr.Body as MemberExpression;
            if (body != null)
            {
                try
                {
                    if (_synchronousInvoker != null)
                        _synchronousInvoker(() => InvokePropertyChanged(expr));
                    else
                        InvokePropertyChanged(expr);
                }
                catch (Exception Ex)
                {
                    if (Ex.Message == "Invoke or BeginInvoke cannot be called on a control until the window handle has been created.")
                    {
                        InvokePropertyChanged(expr);
                    }
                }
            }
        }
 
        private void InvokePropertyChanged<T>(Expression<Func<T>> expr)
        {
            var body = expr.Body as MemberExpression;
            if (body != null)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(body.Member.Name));
                }
            }
        }
 
        #endregion
    }
 
    public class EmployeeCollection : ObservableCollection<Employee>
    {
        public EmployeeCollection()
        {
        }
    }

1 Answer, 1 is accepted

Sort by
0
Maya
Telerik team
answered on 21 Mar 2012, 09:03 AM
Hi David,

Generally, the context of the data field will be 'Address' and if you want to display its properties you can define them as follows:

<TextBlock Text="{Binding Address.City}"  />

I am attaching a sample project illustrating the suggested approach. Could you take a look at it and let me know whether this is the behavior that you want ?  

Kind regards,
Maya
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
Tags
DataForm
Asked by
David
Top achievements
Rank 2
Answers by
Maya
Telerik team
Share this question
or