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

Error setting property on data template selector

7 Answers 99 Views
JumpList
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Paulo Morgado
Top achievements
Rank 1
Paulo Morgado asked on 16 Aug 2012, 02:22 AM
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?




7 Answers, 1 is accepted

Sort by
0
Todor
Telerik team
answered on 16 Aug 2012, 08:48 AM
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.

0
Paulo Morgado
Top achievements
Rank 1
answered on 16 Aug 2012, 09:00 AM
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?
0
Todor
Telerik team
answered on 16 Aug 2012, 10:24 AM
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.

0
Paulo Morgado
Top achievements
Rank 1
answered on 16 Aug 2012, 08:28 PM
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.

0
Todor
Telerik team
answered on 22 Aug 2012, 08:32 AM
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.

0
Paulo Morgado
Top achievements
Rank 1
answered on 22 Aug 2012, 08:43 AM
I've also found that changing the virtualization strategy forces items to be realized again.
0
Todor
Telerik team
answered on 22 Aug 2012, 08:47 AM
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.

Tags
JumpList
Asked by
Paulo Morgado
Top achievements
Rank 1
Answers by
Todor
Telerik team
Paulo Morgado
Top achievements
Rank 1
Share this question
or