zirkelc
zirkelc

Reputation: 1723

Windows Phone - Extend existing Style with Binding

I have a Style defined in my App.xaml file:

    <Style x:Key="TileListBoxItemStyle" TargetType="ListBoxItem">
        <Setter Property="Padding" Value="0"/>
        <Setter Property="Margin" Value="12,12,0,0"/>
        <Setter Property="Background" Value="{StaticResource PhoneAccentBrush}"/>
        <Setter Property="Foreground" Value="White"/>
        <Setter Property="Width" Value="210"/>
        <Setter Property="Height" Value="210"/>
        <!--<Setter Property="HorizontalAlignment" Value="Left"/>-->
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem">
                    <Grid>
                        <Rectangle Fill="{TemplateBinding Background}"/>
                        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

Now I need to change the Background property with a Binding within my PhoneApplicationPage XAML file:

<UserControl.Resources>
    <Style x:Key="TileListBoxItemStyle2" TargetType="ListBoxItem" BasedOn="{StaticResource TileListBoxItemStyle}">
        <Setter Property="Background" Value="{Binding Color}" />
    </Style>
</UserControl.Resources>

The following Exception is thrown:

{System.Windows.Markup.XamlParseException: Failed to assign to property         'System.Windows.Setter.Value'. [Line: 23 Position: 49] ---> System.NotSupportedException: Cannot set read-only property ''.
       at MS.Internal.XamlMemberInfo.SetValue(Object target, Object value)
       at MS.Internal.XamlManagedRuntimeRPInvokes.SetValue(XamlTypeToken inType, XamlQualifiedObject& inObj, XamlPropertyToken inProperty, XamlQualifiedObject& inValue)

This does not work although I searched the web and it seems to work, but maybe there's a limitation of Windows Phone?

Upvotes: 0

Views: 1225

Answers (1)

Benoit Catherinet
Benoit Catherinet

Reputation: 3345

It's not possible to have a binding in a Style setter in windows phone but maybe this article can help you.
Otherwise the only thing I see you can do is create a class inheriting ListBox, orveride GetContainerForItemOverride and create the binding in code.

Upvotes: 3

Related Questions