【问题标题】:Windows Phone - Extend existing Style with BindingWindows Phone - 使用绑定扩展现有样式
【发布时间】:2013-09-21 00:10:40
【问题描述】:

我的 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 的限制?

【问题讨论】:

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


    【解决方案1】:

    不可能在 Windows Phone 的样式设置器中进行绑定,但也许 article 可以帮助您。
    否则我看到你唯一能做的就是创建一个继承 ListBox 的类,或者使用 GetContainerForItemOverride 并在代码中创建绑定。

    【讨论】:

      猜你喜欢
      • 2013-12-13
      • 1970-01-01
      • 2014-08-27
      • 1970-01-01
      • 1970-01-01
      • 2021-05-16
      • 2013-07-29
      • 1970-01-01
      • 2014-01-15
      相关资源
      最近更新 更多