Error setting property on data template selector

8 posts, 0 answers
  1. Paulo Morgado
    Paulo Morgado avatar
    8 posts
    Member since:
    Jan 2010

    Posted 15 Aug 2012 Link to this post

    I have this template selector:

    public class ContactListItemViewModeDataTemplateSelector : DataTemplateSelector
    {
        public DataTemplate ListModeItemTemplate { get; set; }
     
        public DataTemplate TileModeItemTemplate { get; set; }
     
        public int? ListViewMode { get; set; }
     
        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            return this.ListViewMode.HasValue && (this.ListViewMode.Value != 0)
                ? this.TileModeItemTemplate
                : this.ListModeItemTemplate;
        }
    }

    But when I try to use it:

    <telerikDataControls:RadJumpList.ItemTemplateSelector>
        <PauloMorgadoControls:ContactListItemViewModeDataTemplateSelector ListViewMode="{Binding ListViewMode}">
            <PauloMorgadoControls:ContactListItemViewModeDataTemplateSelector.ListModeItemTemplate>
                <DataTemplate>
                    ...
                </DataTemplate>
            </PauloMorgadoControls:ContactListItemViewModeDataTemplateSelector.ListModeItemTemplate>
            <PauloMorgadoControls:ContactListItemViewModeDataTemplateSelector.TileModeItemTemplate>
                <DataTemplate>
                    ...
                </DataTemplate>
            </PauloMorgadoControls:ContactListItemViewModeDataTemplateSelector.TileModeItemTemplate>
        </PauloMorgadoControls:ContactListItemViewModeDataTemplateSelector>
    </telerikDataControls:RadJumpList.ItemTemplateSelector>

    I get this exception:

    Set property '...Controls.ContactListItemViewModeDataTemplateSelector.ListViewMode' threw an exception. [Line: 68 Position: 83]

    If I set an explicit literal value on the ListViewMode property, it works fine.

    What am I doing wrong?




  2. Todor
    Admin
    Todor avatar
    778 posts

    Posted 16 Aug 2012 Link to this post

    Hello Paulo,

    If I understand correctly, the ListViewMode is a property that is contained in your items. If this is the case then this explains why the binding doesn't work. The definition of the ContactListItemViewModeDataTemplateSelector is common for the template selector and is not per item so you can't use a value of some of the items' property in the definition. What you can do is use the item parameter in a way similar to this:
    int? listViewMode = (item as MyDataViewModelItem).ListViewMode;
    return listViewMode.HasValue && (listViewMode.Value != 0) ? this.TileModeItemTemplate : this.ListModeItemTemplate;
    I hope this information helps. If this is not the case please clarify how you set the value of the ListViewMode property.

    Kind regards,
    Todor
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  3. DevCraft banner
  4. Paulo Morgado
    Paulo Morgado avatar
    8 posts
    Member since:
    Jan 2010

    Posted 16 Aug 2012 Link to this post

    Hello Todor,

    The ListViewMode property I'm trying to bind to is a property of the view model bound to the DataContext of the UserControl containing the jump list.

    Does the ListViewMode property of the data template selector need to be a dependency property?
  5. Todor
    Admin
    Todor avatar
    778 posts

    Posted 16 Aug 2012 Link to this post

    Hello Paulo,

    In that case, yes, the ListViewMode property in the DataTemplateSelector should be dependency.

    Don't hesitate to write us back in case you need additional assistance.

    Kind regards,
    Todor
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  6. Paulo Morgado
    Paulo Morgado avatar
    8 posts
    Member since:
    Jan 2010

    Posted 16 Aug 2012 Link to this post

    That solved the problem of setting the value the first time, but the I need the list to be redrawn.

    I tried this:
    private RadJumpList list;
     
    public static readonly DependencyProperty ListViewModeProperty = DependencyProperty.Register(
        "ListViewMode",
        typeof(int?),
        typeof(ContactListItemViewModeDataTemplateSelector),
        new PropertyMetadata(null, ListViewModePropertyChangedCallback));
     
    public int? ListViewMode
    {
        get { return (int?)(this.GetValue(ListViewModeProperty)); }
        set { this.SetValue(ListViewModeProperty, value); }
    }
     
    private static void ListViewModePropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var selector = d as ContactListItemViewModeDataTemplateSelector;
     
        if ((selector != null) && (selector.list != null))
        {
            // redraw list
        }
    }

    But I don't know what to call to force a redraw. It's starting to look that I need 2 lists and make them visible one at a time.

  7. Todor
    Admin
    Todor avatar
    778 posts

    Posted 22 Aug 2012 Link to this post

    Hello Paulo,

    Please accept my appologies for the delayed reply.

    To be as fast as it is, RadJumpList uses virtualization. The items in the collection are realized in a viewport before you can see them on the screen. The ItemTemplateSelector's SelectTempalte method is called when an item is realized or when the collection is changed. You can't call it explicitly, but you can modify the collection, in order to force the templates to be reevaluated. For example, you can reset the collection, which will cause the items to be realized again and the ItemTemplateSelector will select the correct template depending on the new value of your ListViewMode property.

    I hope this information helps.

    Kind regards,
    Todor
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  8. Paulo Morgado
    Paulo Morgado avatar
    8 posts
    Member since:
    Jan 2010

    Posted 22 Aug 2012 Link to this post

    I've also found that changing the virtualization strategy forces items to be realized again.
  9. Todor
    Admin
    Todor avatar
    778 posts

    Posted 22 Aug 2012 Link to this post

    Hi Paulo,

    Yes, the VirtualizationStrategy is also part of the virtualization mehanizm and changing it will do the trick for your scenario.

    Don't hesitate to write us back if you have further questions.

    Kind regards,
    Todor
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Back to Top
DevCraft banner