Click here to Skip to main content
Rate this: bad
good
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}">
                        <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 Smile | :) Thanks in advance!
Posted 9-Apr-13 7:52am

1 solution

Rate this: bad
good
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">
         <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>
  Permalink  
v5
Comments
Andre Pieterse at 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 at 9-Apr-13 13:23pm
   
it exists on the ListBox, you can try to use ListBox instead of ItemsControl.
Andre Pieterse at 9-Apr-13 13:51pm
   
I can't use ListBox because it needs to lay out my items in a Grid?
Irina Pykhova at 9-Apr-13 14:01pm
   
Why not, it uses ItemsPanel exactly as ItemsControl
Andre Pieterse at 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!
Irina Pykhova at 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 at 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 Sergey Alexandrovich Kryukov 535
1 OriginalGriff 275
2 BillWoodruff 260
3 Shweta N Mishra 244
4 Deepu S Nair 230
0 OriginalGriff 6,168
1 Sergey Alexandrovich Kryukov 5,818
2 DamithSL 4,958
3 Manas Bhardwaj 4,539
4 Maciej Los 3,755


Advertise | Privacy | Mobile
Web03 | 2.8.1411019.1 | Last Updated 10 Apr 2013
Copyright © CodeProject, 1999-2014
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