Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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).

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


I am new to Silverlight, hope it is not a dumb question :) Thanks in advance!
Posted

1 solution

1. Silverlight has different syntax for Style.TargetType property, use
HTML
<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:
HTML
<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">
         <Setter.Value>
             <ControlTemplate TargetType="ListBoxItem">
                 <Grid Background="{TemplateBinding Background}">
                     <ContentPresenter x:Name="contentPresenter"
                           Content="{TemplateBinding Content}"
                           ContentTemplate="{TemplateBinding ContentTemplate}"
                           HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                           Margin="{TemplateBinding Padding}"/>
                 </Grid>
             </ControlTemplate>
         </Setter.Value>
     </Setter>
</Style>
 
Share this answer
 
v5
Comments
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.

Thanks!

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900