Generate PropertyDefinitions in Code

8 posts, 0 answers
  1. Peter
    Peter avatar
    28 posts
    Member since:
    Oct 2006

    Posted 11 Aug 2011 Link to this post

    Hello,

    today I was working on a project, where I try to generate my PropertyGrid's PropertyDefinitions by code. While working on this I was not able to get the definitions to use a specified binding. I have a class called MainViewModel which has 3 properties. Using reflection I iterate through them:
    var vm = new MainViewModel() { FirstName = "John", Name = "Doe", DateOfBirth = DateTime.Parse("01.01.1980")};
     
                var properties = new List<PropertyDefinition>();
     
     
                PropertyInfo[] propertyInfos = typeof(MainViewModel).GetProperties();
     
                foreach(var info in propertyInfos)
                {
                    var def = new PropertyDefinition(new ItemPropertyInfo(info.Name, info.PropertyType, ""))
                                  {
                                      Binding = new Binding(info.Name) {Mode = BindingMode.TwoWay},
                                      EditorTemplate = textTemplate
                                  };
     
     
                    properties.Add(def);
                }
     
                foreach(var definition in properties)
                {
                    propertyGrid.PropertyDefinitions.Add(definition);
                }
     
                propertyGrid.Item = vm;

     

    At the end I set the item to the ViewModel as otherwise there will be no data displayed. The textTemplate is just a TextBox with Text="{Binding}" to show the type of the bound property. But instead of showing text and dates it displays the ViewModel's type name (see screenshot).

    How can I get this working?

    Best regards,
    Peter

  2. Pavel Pavlov
    Admin
    Pavel Pavlov avatar
    2039 posts

    Posted 17 Aug 2011 Link to this post

    Hello Peter,

    Although you set the full path at the binding for the PropertyDefinition,  later you override this binding in the template - as you said : TextBox with Text="{Binding}".
    The DataContext is  your view model and it is expected to show the ToString() of your ViewModel.

    The solution would be to specify the full path in the binding of the textbox ... for example : 
    Text="{Binding Path=FirstName}".

    This will show you the FirstName property instead of the ViewModel's type name .

    All the best,
    Pavel Pavlov
    the Telerik team

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

  3. DevCraft banner
  4. Peter
    Peter avatar
    28 posts
    Member since:
    Oct 2006

    Posted 17 Aug 2011 Link to this post

    Hello Pavlov,

    thanks for the reply. I am afraid that my explanation was not clear enough. I know that I have to specifiy a PropertyPath to bind to a Property. But what I want to build is one template which can render different properties (of the same type, of source). In code-behind I specify which one I render (see PropertyDefinition.Binding = new Binding() ... ). The TextBox was just a way to see what was bound to my template and it was always the ViewModel instead of one of its properties.

    You know what I mean?

    Best regards,
    Peter
  5. Pavel Pavlov
    Admin
    Pavel Pavlov avatar
    2039 posts

    Posted 23 Aug 2011 Link to this post

    Hello Peter,

    Well the DataContext of the fields would be your business object. So by means of XAML we do not have much choice except for hardcoding the binding path .

    An alternative would be to create the template programmatically (e.g. XamlReader.Load)  and alter only the path-related part of the XAML string for each separate field. 

    In case you need a sample on that - just let me know.

    Kind regards,
    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 >>

  6. ramesh
    ramesh avatar
    5 posts
    Member since:
    Feb 2011

    Posted 24 Aug 2011 Link to this post

    Hi this is Ramesh,

    what ever you are saying exactly correct, but i want one doubt ...how to create editor Template in code behind?

    please send me the code for above.

    thanks in advance.
  7. Peter
    Peter avatar
    28 posts
    Member since:
    Oct 2006

    Posted 25 Aug 2011 Link to this post

    Hi Ramesh,

    it is quite easy: Use a StringBuilder to create the XAML and send it through the XamlReader. The approach is not the prettiest feature of Silverlight but it works. Additionally you can make changes in the text with your normal C# code. If you want another control to edit the property just replace TextBox with the control you want. Here is the code:

    var sb = new StringBuilder();
    sb.Append("<DataTemplate ");
    sb.Append("xmlns:controls='clr-namespace:PoCo.Core.SL.Controls;assembly=PoCo.Core.SL' ");
    sb.Append("<Grid>");
    sb.Append(@"<TextBox Text=""{Binding PropertyName,Mode=TwoWay}"" />");
    sb.Append("</Grid>");
    sb.Append("</DataTemplate>");
     
    DataTemplate dt = (DataTemplate)XamlReader.Load(sb.ToString());

    This is the way I made it work. Hope this is helpful to you.

    Best regards,
    Peter
  8. ramesh
    ramesh avatar
    5 posts
    Member since:
    Feb 2011

    Posted 25 Aug 2011 Link to this post

    Hi, am trying the what ever u saying code but still no data displays

    please go through the  below code and also see the  screenshot also

     void MainPage_Loaded(object sender, RoutedEventArgs e)
            {            
                var properties = new List<PropertyDefinition>();
                foreach (var info in propertyInfos)
                {
                    //one way of data template
                    var sb = new StringBuilder();
                    sb.Append("<DataTemplate ");
                    sb.Append("xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' ");
                    sb.Append("xmlns:controls='clr-namespace:PoCo.Core.SL.Controls;assembly=PoCo.Core.SL' ");
                    sb.Append("xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>");
                    sb.Append("<Grid>");
                    sb.Append(@"<TextBox Text=""{Binding objectValue,Mode=TwoWay}"" />");
                    sb.Append("</Grid>");
                    sb.Append("</DataTemplate>");

                    DataTemplate dt = (DataTemplate)XamlReader.Load(sb.ToString());


                    //other way of data template
                    DataTemplate dataTemplate = XamlReader.Load(@"<DataTemplate" +
            " xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" " +
            " xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\">" +
            "<TextBox Text=\"{Binding}\"></TextBox>" +
            "</DataTemplate>") as DataTemplate;               


                    PropertyDefinition definition = new PropertyDefinition()
                    {
                        //Binding = new Binding("objectValue"),
                        EditorTemplate = dt,
                        DisplayName = info.displayName,
                        GroupName = "Emp"
                    };
                    properties.Add(definition);
                }
                foreach (var definition in properties)
                {
                    mainPropertyGrid2.PropertyDefinitions.Add(definition);
                }
                mainPropertyGrid2.Item = propertyInfos;
            }
      
  9. Pavel Pavlov
    Admin
    Pavel Pavlov avatar
    2039 posts

    Posted 25 Aug 2011 Link to this post

    Hi Ramesh,

    Please check your output window for binding expression errors.

    Greetings,
    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 >>

Back to Top
DevCraft banner