【问题标题】:Binding to style in my WP7 Silverlight Application在我的 WP7 Silverlight 应用程序中绑定到样式
【发布时间】:2011-02-03 20:18:46
【问题描述】:

在我看来,我有一个按钮:

<Button Style="{StaticResource ButtonTextForwardStyle}" Content="My Text" Width="400" Height="100" />

在 app.xaml 中,我试图定义一个看起来像

的样式

<ControlTemplate x:Key="ButtonTextForwardTemplate" TargetType="Button">
    <Grid>
        <vsm:VisualStateManager.VisualStateGroups>
            <vsm:VisualStateGroup x:Name="CommonStates">
                <vsm:VisualState x:Name="Normal"/>
                <vsm:VisualState x:Name="Pressed"/>
            </vsm:VisualStateGroup>
        </vsm:VisualStateManager.VisualStateGroups>
    </Grid>
</ControlTemplate>
<Style x:Key="ButtonTextForwardStyle" TargetType="Button">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Grid>
                    <vsm:VisualStateManager.VisualStateGroups>
                        <vsm:VisualStateGroup x:Name="CommonStates">
                            <vsm:VisualState x:Name="Normal"/>
                            <vsm:VisualState x:Name="Pressed">
                                <Storyboard Storyboard.TargetName="image" Storyboard.TargetProperty="Source">
                                    <ObjectAnimationUsingKeyFrames>
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="pressedimage.png"/>
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </vsm:VisualState>
                        </vsm:VisualStateGroup>
                    </vsm:VisualStateManager.VisualStateGroups>
                    <StackPanel Orientation="Horizontal">
                        <Image x:Name="image" Source="normalimage.png" Height="80" Width="100" Stretch="Fill" VerticalAlignment="Center"/>
                        <TextBlock Text="{Binding Content}" VerticalAlignment="Center" />
                    </StackPanel> 
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

我希望将按钮的内容(我的文本)绑定到 TextBlock。但它看起来好像不起作用。

我该怎么做?

提前感谢您的帮助。

最好的问候

【问题讨论】:

    标签: silverlight data-binding windows-phone-7


    【解决方案1】:

    您的 TextBlock 位于 Button ControlTemplate 内,因此您可能希望使用 TemplateBinding 而不是 Binding。

    <TextBlock Text="{TemplateBinding Content}"
    

    【讨论】:

    • 对于其他属性,高度和宽度应该是一样的,不是吗?
    猜你喜欢
    • 2011-09-13
    • 2011-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-20
    • 2011-05-13
    • 2011-05-08
    相关资源
    最近更新 更多