locked
What am I missing here ? RRS feed

  • Question

  • Hi Experts,

    This is what im trying to do.

    I have created a user control with DataGrid coltrol which implements INotifyPropertyChanged. Added a Textblock to display the title of the grid. text property of the Textblock is binded to a CLR title property within the same control.

    I have added this unse control to another XAML view and binded a list of items and the grid populates it correctly. Also i'm setting the title of the control within the calling xaml window but I dont see the title.

    User Control's Title property 

            private string title;
            /// <summary>
            /// Get and Set the Title of the Control
            /// </summary>
            public string Title
            {
                get
                {
                    return title;
                }
                set
                {
                    title = value;
                    PropertyValueChanged("Title");
                }
            }

    How the list is generated for binding

    public class StaffInformation
        {
            public enum Genders
            {
                Unknown,
                Male,
                Female,
            }
    
            [HeaderName("First Name"), ColumnIndex("0")]
            public string FirstName { get; set; }
    
            [HeaderName("Last Name"), ColumnIndex("2")]
            public string LastName { get; set; }
    
            [HeaderName("Email Address"), ColumnIndex("3")]
            public string Email { get; set; }
    
            //[HeaderName("Gender"), ColumnIndex("1")]
            //public Genders Gender { get; set; }
    
            [HeaderName("City"), ColumnIndex("4")]
            public string City { get; set; }
    
            [HeaderName("Profession"), ColumnIndex("5")]
            public string Profession { get; set; }
    
        }
    
        public class BindingCollection
        {
            public List<StaffInformation> StaffDetails
            {
                get
                {
                    List<StaffInformation> staffInfo = new List<StaffInformation>();
                    staffInfo.Add(new StaffInformation { City = "Colombo", Email = "kur@gmail.com", FirstName = "Kuru", LastName = "Thiya", Profession = "Eng" });
    
                    return staffInfo;
                }
            }
    
            public string Title
            {
                get
                {
                    return "Staff Details";
                }
            }
        }

    How the DataContext is set to user control from calling xaml view's code behind

    private void PopulateDataGrid() { BindingCollection bc = new BindingCollection();

    //Setting the datacontext of user control StaffDetailsGrid.DataContext = bc.StaffDetails; StaffDetailsGrid.Title = bc.Title; }



    • Edited by WPF_Boy Thursday, January 10, 2013 11:02 AM
    Thursday, January 10, 2013 11:00 AM

Answers

  • I believe you should be using StaffDetailsGrid.ItemsSource, not DataContext.

    Regards,
    Pete


    #PEJL

    • Marked as answer by Sheldon _Xiao Tuesday, January 15, 2013 6:53 AM
    Thursday, January 10, 2013 11:49 AM
  • Hi Sheldon,

    Sorry for not updating the thread. I have fixed it. Problem was with the data context i had set. 


    • Marked as answer by Sheldon _Xiao Tuesday, January 15, 2013 6:53 AM
    Tuesday, January 15, 2013 6:51 AM

All replies

  • I believe you should be using StaffDetailsGrid.ItemsSource, not DataContext.

    Regards,
    Pete


    #PEJL

    • Marked as answer by Sheldon _Xiao Tuesday, January 15, 2013 6:53 AM
    Thursday, January 10, 2013 11:49 AM
  • And there's no property named:

    StaffDetailsGrid.Title= bc.Title;



    JP Cowboy Coders Unite!

    Thursday, January 10, 2013 12:18 PM
  • Hi WPF_Boy,

    How about your issue now?

    If your issue persists, could you share your DataGrid xaml code, I want to know how do you bind the ItemsSource property.

    Best regards,


    Sheldon _Xiao
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, January 15, 2013 6:42 AM
  • Hi Sheldon,

    Sorry for not updating the thread. I have fixed it. Problem was with the data context i had set. 


    • Marked as answer by Sheldon _Xiao Tuesday, January 15, 2013 6:53 AM
    Tuesday, January 15, 2013 6:51 AM