views:

146

answers:

1

Hi,

I have the following XAML, which works fine in WPF, but not in Silverlight 4

       <ItemsPanelTemplate x:Key="ContentListBoxItemsPanelTemplate">
            <Canvas/>
        </ItemsPanelTemplate>

        <DataTemplate x:Key="ContentListBoxItemTemplate">
            <Border CornerRadius="15" Width="150" Margin="3" Height="300">
                <Border.Background>
                    <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                        <GradientStop Color="OrangeRed" Offset="1" />
                        <GradientStop Color="Brown" Offset="0" />
                    </LinearGradientBrush>
                </Border.Background>
            </Border>
        </DataTemplate>

        <Style TargetType="ListBoxItem">
            <Setter Property="Canvas.Left" Value="{Binding Left}"/>
            <Setter Property="Canvas.Top" Value="{Binding Top}"/>
        </Style>

And then somewhere:

 <ListBox Name="ContentList" 
          ItemTemplate="{StaticResource ContentListBoxItemTemplate}"
          ItemsPanel="{StaticResource ContentListBoxItemsPanelTemplate}" />

If I try the same thing in Silverlight I get an exception saying that the setter cannot set a read only property, but I still want to achieve the same thing in Silverlight without code.

Any suggestions?

+3  A: 

Silverlight does not support bindings in the value of the setter. David Anson has a great workaround here: http://blogs.msdn.com/delay/archive/2009/05/07/one-more-platform-difference-more-or-less-tamed-settervaluebindinghelper-makes-silverlight-setters-better.aspx

Jeremiah Morrill
I like the link, but that gives me: Unable to access DependencyProperty "Canvas.Top" on type "ListBoxItem". thrown by the SetterValueBindingHelper class
TimothyP
Ah it seems I forgot the Type parameter. But still it does not work 100% only the last setter works... contacted the author, if he can fix this of course I'll mark your answer as the solution
TimothyP
There is an updated version here that solves it http://blogs.msdn.com/delay/archive/2009/11/02/as-the-platform-evolves-so-do-the-workarounds-better-settervaluebindinghelper-makes-silverlight-setters-better-er.aspx
TimothyP