locked
run time exception: 'StaticResource mySelector' is not a valid value for property 'ItemTemplateSelector' RRS feed

  • Question

  • trying to use the ItemTemplateSelector property of ListBox to control which DataTemplate is bound to a ListBox Item.

    When I run the code getting an exception:
    'StaticResource mySelector' is not a valid value for property 'ItemTemplateSelector'

    How to use code to control which data template to apply to the binding to items in a ListBox?

    Here is the code:

        <ListBox x:Name="lstItems" Grid.Row="1" Grid.Column="0"
                 ItemTemplateSelector="StaticResource mySelector"
                 ItemsSource="{Binding SystemList}">
       </ListBox>
    

    <Window x:Class="WpfWrkmbrpdm.Windows.SelectSystemWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:local="clr-namespace:WpfWrkmbrpdm.Windows"
            Title="SelectSystemWindow" Height="400" Width="400">
    

    associate "mySelector" with the type TaskListDataTemplateSelector.

    Also, define a DataTemplate with a Name and DataType. I want this template to be the default template.

        <Window.Resources>
            <local:TaskListDataTemplateSelector x:Key="mySelector" />
            
            <DataTemplate x:Name="defaultTemplate" DataType="{x:Type as:As400System}">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="auto"></RowDefinition>
                    </Grid.RowDefinitions>
    
                    <StackPanel Orientation="Horizontal"
                            Grid.Row="0" Grid.Column="1">
                        <Label>System name</Label>
                        <TextBox Text="{Binding SystemName}"
                           MinWidth="60" Margin="3"/>
                    </StackPanel>
                </Grid>
            </DataTemplate>
        </Window.Resources>
    
    The code for the DataTemplateSelector class:
      public class TaskListDataTemplateSelector : DataTemplateSelector
      {
        public override DataTemplate
            SelectTemplate(object item, DependencyObject container)
        {
          FrameworkElement element = container as FrameworkElement;
    
          if (element != null && item != null && item is As400System)
          {
            var taskitem = item as As400System;
    
            if (taskitem.SystemName == "Add")
              return
                  element.FindResource("importantTaskTemplate") as DataTemplate;
            else
              return
                  element.FindResource("defaultTemplate") as DataTemplate;
          }
    
          return null;
        }
      }
    

    Monday, October 22, 2012 4:28 PM

Answers

  • Steve,

    Just wrap around with curly brackets

    ItemTemplateSelector="{StaticResource mySelector}"

    Hope this helps.


    noorbakhsh حميد نوربخش

    • Proposed as answer by Pete LakerMVP Monday, October 22, 2012 7:20 PM
    • Marked as answer by Steve Richter Monday, October 22, 2012 8:37 PM
    Monday, October 22, 2012 4:51 PM