Click here to Skip to main content
11,921,353 members (61,986 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# XAML WPF Silverlight
Hi. I am struggling with something in silverlight while trying to port some code from WPF. I have an ObservableCollection in my ViewModel called Fields, which is bound to the ItemsSource of an ItemsControl. The ItemsPanelTemplate is set to a grid with bindings through a helper class. The problem is with the ItemContainerStyle, it does not work in Silverlight (5).

<ItemsControl ItemsSource="{Binding Path=Fields}">
                                <Grid local:GridHelper.RowCount="{Binding Path=RowCount}" local:GridHelper.ColumnCount="{Binding Path=ColumnCount}"/>
                            <Style TargetType="{x:Type FrameworkElement}">
                                <Setter Property="Grid.Row" Value="{Binding Row}"/>
                                <Setter Property="Grid.Column" Value="{Binding Column}"/>

I am new to Silverlight, hope it is not a dumb question Smile | :) Thanks in advance!
Posted 9-Apr-13 7:52am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

1. Silverlight has different syntax for Style.TargetType property, use
<style targettype="FrameworkElement"></style>
(Style.TargetType Property).

2. Binding in the property setters has been added in Silverlight 5. So if you use SL4, it won't work.

3. There is no ItemContainerStyle property on the ItemsControl. It exists on derived controls such as ListBox. If you don't need some ListBox-specific behavior, such as selection, use simplified ListBoxItem control template, for example:
<Style TargetType="ListBoxItem">
     <Setter Property="Grid.Row" Value="{Binding Row}"/>
     <Setter Property="Grid.Column" Value="{Binding Column}"/>
     <Setter Property="HorizontalAlignment" Value="Stretch"/>
     <Setter Property="VerticalAlignment" Value="Stretch"/>     
     <Setter Property="HorizontalContentAlignment" Value="Stretch"/>                    
     <Setter Property="VerticalContentAlignment" Value="Stretch"/>                    
     <Setter Property="Template">
             <ControlTemplate TargetType="ListBoxItem">
                 <Grid Background="{TemplateBinding Background}">
                     <ContentPresenter x:Name="contentPresenter"
                           Content="{TemplateBinding Content}"
                           ContentTemplate="{TemplateBinding ContentTemplate}"
                           HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                           Margin="{TemplateBinding Padding}"/>
Andre Pieterse 9-Apr-13 13:18pm
Hi Irina,

Thanks for the reply, but it doesn't fix the problem. It seems like the ItemContainerStyle does not exist in Silverlight? I think I need a workaround, by maybe using ItemsControl.ItemTemplate instead of ItemsControl.ItemContainerStyle? I just can't figure it out :(
Irina Pykhova 9-Apr-13 13:23pm
it exists on the ListBox, you can try to use ListBox instead of ItemsControl.
Andre Pieterse 9-Apr-13 13:51pm
I can't use ListBox because it needs to lay out my items in a Grid?
Irina Pykhova 9-Apr-13 14:01pm
Why not, it uses ItemsPanel exactly as ItemsControl
Andre Pieterse 10-Apr-13 13:03pm
Ok, I see what you mean. It helps, but it's not perfect yet, now the items gets highlighted when you move the mouse over the ListBox. Also, the items should stretch to fill the grid columns. Any Ideas? I'll accept your solution if you can give me advice on this.

Irina Pykhova 10-Apr-13 13:44pm
I suppose, you should set alignment properties in the ListBoxItem style and use custom control template for it, which doesn't show selection and focus on items. I added sample in my solution above
Andre Pieterse 10-Apr-13 15:56pm
You sir, are a genius! Thanks so much :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 553
1 Dave Kreskowiak 245
2 phil.o 160
3 KrunalRohit 134
4 Sergey Alexandrovich Kryukov 132
0 OriginalGriff 6,951
1 KrunalRohit 4,530
2 Sergey Alexandrovich Kryukov 3,171
3 George Jonsson 2,840
4 Suvendu Shekhar Giri 2,181

Advertise | Privacy | Mobile
Web01 | 2.8.151120.1 | Last Updated 10 Apr 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100