Problem with EditorTemplateSelector

4 posts, 1 answers
  1. Marcel
    Marcel avatar
    21 posts
    Member since:
    Jul 2011

    Posted 28 Oct 2014 Link to this post

    Hi,

    I can't figure out how to correctly use the EditorTemplateSelector with the RadPropertyGrid. I just want to apply custom DataTemplates for different types of properties. The applying parts works as expected but I don't understand how to get the DataContext to work. DataContext inside the DataTemplates is always pointing to the top level object that is bound to the RadPropertGrid as Item. It would really make more sense to me to have the specific property (or PropertyDefinition) as DataContext. So, how do I bind to the specific property from the Editor-DataTemplate?

    Thanks for your help!

    Marcel
  2. Answer
    Boris
    Admin
    Boris avatar
    276 posts

    Posted 30 Oct 2014 Link to this post

    Hello Marcel,

    A possible way for you to apply a DataTemplate depending only on the type of your property and not worry about the binding is to use our AutoBind Attached Behavior. For more information you can check the DataTemplateSelector Support article as well as our DataTemplateSelectors WPF / Silverlight demo and AutoBindAttachedBehavior sdk example. 

    On a side note, to easily browse and run our sdk examples, you can use our SDK Sample Browser.

    I attached a sample project that demonstrates the suggested approach.

    I hope this helps.

    Regards,
    Boris Penev
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  3. UI for WPF is Visual Studio 2017 Ready
  4. Marcel
    Marcel avatar
    21 posts
    Member since:
    Jul 2011

    Posted 31 Oct 2014 in reply to Boris Link to this post

    Hi Boris,

    thank you very much for your help. I think I forgot to mention that probably the cause of my problem were properties of complex objects from my object model. With the help of AutoBindBehavior.UpdateBindingOnElementLoaded together with your example I finally got it working. Still one thing is left that confuses me a little bit. Everything is fine if I do this:

    <DataTemplate>
        <Grid>
            <StackPanel Orientation="Vertical">
                <TextBox telerik:AutoBindBehavior.UpdateBindingOnElementLoaded="DataContext" Text="{Binding Text}"/>
                <TextBox telerik:AutoBindBehavior.UpdateBindingOnElementLoaded="DataContext" Text="{Binding Text2}"/>
            </StackPanel>
        </Grid>
    </DataTemplate>

    But if I change it in a way the would be more conform to my understanding of WPF data binding, it does not work and throws binding errors:

    <DataTemplate>
        <Grid telerik:AutoBindBehavior.UpdateBindingOnElementLoaded="DataContext">
            <StackPanel Orientation="Vertical">
                <TextBox Text="{Binding Text, UpdateSourceTrigger=PropertyChanged}"/>
                <TextBox Text="{Binding Text2}"/>
            </StackPanel>
        </Grid>
    </DataTemplate>

    Looks like the DataContext is not inherited for whatever reason...

    Regards,
    Marcel
  5. Boris
    Admin
    Boris avatar
    276 posts

    Posted 05 Nov 2014 Link to this post

    Hi Marcel,

    The way in which you use the AutoBindBehavior of the PropertyGrid seams unnatural. Since the AutoBindBehavior is meant to be bound to the main edit property of the editor and not to its DataContext. For example you can bind the AutoBindBehavior to the Text property of the TextBox editor. However, we haven't experimented with binding to the DataContext of the editor.

    In order to provide you with a better answer on why the code from your first code-snippet works and the other does not, could you please provide us with a small runnable project? By providing us with such a project we will be able to debug it on our side and inform you of our findings. 

    Regards,
    Boris
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
Back to Top