Windows Phone - 使用绑定扩展现有样式

时间:2013-09-16 18:31:31

标签: c# xaml binding windows-phone-8 windows-phone

我在App.xaml文件中定义了一个样式:

    <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>

现在我需要在PhoneApplicationPage XAML文件中使用Binding更改Background属性:

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

抛出以下异常:

{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)

虽然我在网上搜索它似乎不起作用,但这可能不起作用,但也许Windows Phone有限制?

1 个答案:

答案 0 :(得分:3)

Windows手机中的Style setter中无法绑定,但article可以帮助您。 否则,我看到你唯一能做的就是创建一个继承ListBox的类,或者在GetContainerForItemOverride中创建,并在代码中创建绑定。