14

I'm trying to create a descendant class from the silverlight toolkit LongListSelector. Let's call it SimpleLonglistSelector. I started from the "Silverlight for Windows Phone Toolkit Source & Sample - Feb 2011.zip"

http://silverlight.codeplex.com/releases/view/60291

I created a new class:

public class SimpleLongListSelector : LongListSelector
{
    public SimpleLongListSelector()
    {
        var itemsPanelTemplate = @"
            <ItemsPanelTemplate xmlns='http://schemas.microsoft.com/client/2007'>
                <toolkit:WrapPanel xmlns:toolkit='clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit' Orientation=""Horizontal""/>
            </ItemsPanelTemplate>";

        this.GroupItemsPanel = (ItemsPanelTemplate)XamlReader.Load(itemsPanelTemplate);

        var groupItemTemplate = @"
            <DataTemplate xmlns='http://schemas.microsoft.com/client/2007'>
                <Border Width=""99"" Height=""99"" Background=""{StaticResource PhoneAccentBrush}"" Margin=""6"" IsHitTestVisible=""{Binding HasItems}"">
                    <TextBlock Text=""{Binding Key}"" 
                                           FontFamily=""{StaticResource PhoneFontFamilySemiBold}""
                                           FontSize=""36""
                                           Margin=""{StaticResource PhoneTouchTargetOverhang}""
                                           Foreground=""{StaticResource PhoneForegroundBrush}""                                        
                                           VerticalAlignment=""Bottom""/>
                </Border>
            </DataTemplate>";

        this.GroupItemTemplate = (DataTemplate)XamlReader.Load(groupItemTemplate);

        var groupHeaderTemplate = @"
            <DataTemplate xmlns='http://schemas.microsoft.com/client/2007'>
                <Border Background=""Transparent"">
                    <Border Background=""{StaticResource PhoneAccentBrush}"" Width=""75"" Height=""75"" HorizontalAlignment=""Left"">
                        <TextBlock Text=""{Binding Path=Key}"" 
                                               Foreground=""{StaticResource PhoneForegroundBrush}"" 
                                               Style=""{StaticResource PhoneTextExtraLargeStyle}""
                                               VerticalAlignment=""Bottom""/>
                    </Border>
                </Border>
            </DataTemplate>";

        this.GroupHeaderTemplate = (DataTemplate)XamlReader.Load(groupHeaderTemplate);

        var itemTemplate = @"
            <DataTemplate xmlns='http://schemas.microsoft.com/client/2007'>
                <TextBlock Text=""{Binding Title}"" FontSize=""30""/>
            </DataTemplate>";

        this.ItemTemplate = (DataTemplate)XamlReader.Load(itemTemplate);
    }
}

Then I added it to the LongListSelector example, in the same pivot as all of the other long list selectors:

            <controls:PivotItem Header="SLLS">
                <local:SimpleLongListSelector x:Name="simple" />
            </controls:PivotItem>

Then I added it's source to be the same as the movies source in the LoadLinqMovies()

        simple.ItemsSource = moviesByCategory;

Then run the code (I know it doesn't look pretty, that's because the bindings haven't been set up right, I do that so you know it's not the data. If you'd like, you can do it like this:

        simple.ItemsSource = movies.GroupBy((m) => m.Title[0]).Select((c) => new PublicGrouping<char, Movie>(c));

That looks like I want it to look.

Well, in either case, this works as expected, except when I click on a group header. (any of the [by default blue] squares). I get a

WrappedException

The error message is:

0xc00cee3c

Which I think means:

well-formedness constraint: unique attribute spec

I don't think I've got a uniqueness problem. What am I doing wrong?

McKay
  • 12,334
  • 7
  • 53
  • 76
  • 1
    Why are you creating a new subclass when all you appear to be doing is changing the data templates? You can change the templates without inheriting. – Matt Lacey Aug 01 '11 at 10:02
  • @Matt Lacey, Among other things, what I want is a standard set of data templates, so I don't have to apply the same templates in the several different places I want to use this control. Reduce Code Duplication. – McKay Aug 03 '11 at 17:08
  • 1
    You can create named templates as resources and reuse them that way – Matt Lacey Aug 04 '11 at 15:20
  • @Matt Lacey, yeah, but I still have to add in those templates (what, are 6 of them required for proper functioning of this control, but you can get by with 4?) each time, and if they're all going to be the same, that's still a lot of duplication. It is a good idea, and it is better than redoing the whole control, but I'd like to get this version working. – McKay Aug 05 '11 at 14:25
  • 2
    Realise this doesn't answer the question, but to solve some of the duplication is rather than have the templates as separate resources declare a style that sets all six templates. Then all you need to do is set the style property once per long list. – Nigel Sampson Oct 10 '11 at 02:29
  • @Nigel Sampson I Think that's what I'm looking for actually. I'll have to give it a shot, I can probably put unique code in an extension method, if I only have to say "hey, here's the style" and it does what I want, that would probably work. Write it up as an answer, if you'd like the credit for it and we'll see. Yeah, it doesn't really solve the question. But at this point (2 1/2 months and a bounty), it looks like no one actually knows what's going on here. – McKay Oct 10 '11 at 13:50
  • Just out of curiosity, if you don't have the outer of the nested `Border` elements, do you still get that error? – AakashM Oct 10 '11 at 16:12
  • @AakashM removing the outer of the nested border elements doesn't change anything :( Good idea thouh. – McKay Oct 13 '11 at 02:25
  • I just tried your sample here by following your instructions, but using the 7.1 toolkit from http://silverlight.codeplex.com/releases/view/71550 and it worked like a champ. I wonder if there is just a bug somewhere in the 7.0 version? – Chris Koenig Oct 18 '11 at 18:11
  • @Chris Koenig Oh, I haven't given this a shot on 7.1 yet. That's a good theory. – McKay Oct 19 '11 at 15:17
  • Did my suggestion work for you? I never heard back... – Chris Koenig Oct 31 '11 at 02:00
  • The plan is to check it, but I haven't migrated to 7.1 yet on this project. I'm planning on checking it out though. – McKay Oct 31 '11 at 15:02

1 Answers1

1

If you use the LongListSelector from the 7.1 toolkit, found at http://silverlight.codeplex.com/releases/view/71550, your sample code works as listed above. This must have been some bug in the original LLS...

Chris Koenig
  • 2,736
  • 18
  • 17