locked
Listbox.ItemTemplate of UserControl not working RRS feed

  • Question

  • I have a problem with my Silverlight Application. I have got a UserControl "MapLayer" which implements an Interface "IMapLayer".

    These "MapLayer"s are hold in an ObersvableCollection. They are also added to the Children of a Grid.

    At the same time I want to show some informations of the MapLayer in a ListBox (because the class has some public properties). I only want to show some informations so I created a ListBox.ItemTemplate which shows via binding the MapLayer-Public-Properties.

    The ItemTemplate looks like this:

                        <ListBox x:Name="ListboxMapLayers" ItemsSource="{Binding}" SelectionChanged="ListboxMapLayers_SelectionChanged" HorizontalAlignment="Stretch" BorderThickness="0">
                            <ListBox.ItemTemplate>
                                <DataTemplate>
                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="*" />
                                            <ColumnDefinition Width="*" />
                                        </Grid.ColumnDefinitions>
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="*" />
                                            <RowDefinition Height="*" />
                                        </Grid.RowDefinitions>
                                        <TextBlock Grid.Row="0" Grid.ColumnSpan="2" Text="{Binding LayerName}" FontWeight="Bold" />
                                        <TextBlock Grid.Row="1" Grid.Column="0" Text="ZIndex: " />
                                        <TextBlock Grid.Row="1" Grid.Column="0" Text="{Binding ZIndex}" />
                                        <TextBlock Grid.Row="1" Grid.Column="1" Text="Tiles: " />
                                        <TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Tiles.Length}" />
                                    </Grid>
                                </DataTemplate>
                            </ListBox.ItemTemplate>
                        </ListBox>

    The code-behind:

    this.ListboxMapLayers.DataContext = this.MapLayers;

    The MapLayers-Property looks like this:

    public ObservableCollection<IMapLayer> MapLayers { get { return this.mapLayers; } }

    And this is the Interface IMapLayer:

    public interface IMapLayer
    {
        string LayerName { get; set; }
        int ZIndex { get; set; }
        bool IsVisible { get; set; }
        ITile[,] Tiles { get; set; }
    }

    Don't forget: The IMapLayer is implemented into a UserControl. This UserControl is added to the Children of a Grid.

    My problem is: Whenever I try to add the ObservableCollection to the DataContext of the ListBox, I got following error:

    System.argumentException: value does not fall within the expected range

    The problem: If I don't add every MapLayer to the Children of the Grid, the ListBox show some data, but the ItemTemplate of the ListBox doesn't work. It just shows the whole Usercontrol in the ListBox.

    How can I realize this: I want to show the MapLayer in the Children of a Grid and at the same time, I want to show the infos of the MapLayer in a ListBox with an ItemTemplate.

    Cheers

    Sunday, February 10, 2013 12:04 PM

All replies

  • http://stackoverflow.com/questions/1697876/data-bound-usercontrol-in-listbox-itemtemplate
    Wednesday, February 27, 2013 3:00 AM
  • Make sure your IMapLayer instances don't all have the same x:Name.
    Monday, March 4, 2013 7:09 AM