带有多个文本块的 Windows 7 Phone 按钮

发布于 2024-10-07 02:46:45 字数 2596 浏览 3 评论 0原文

我通过编辑其样式在 Blend 中创建了一个按钮。我添加了多个文本块,目的是向用户实时显示数据。但是,我不知道如何与后面的代码中的这些文本块进行交互。

我的样式 XAML 是这样的:

<ControlTemplate TargetType="Button">
                        <Grid Background="Transparent">
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="CommonStates">
                                    <VisualState x:Name="Normal"/>
                                    <VisualState x:Name="MouseOver"/>
                                    <VisualState x:Name="Pressed"/>
                                    <VisualState x:Name="Disabled"/>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                            <Border x:Name="ButtonBackground" BorderThickness="{TemplateBinding BorderThickness}" Background="{StaticResource PhoneAccentBrush}" CornerRadius="0" Margin="8,12,12,12">
                                <TextBlock Margin="121,5,98,0" TextWrapping="Wrap" Text="Current Program:" Height="36" VerticalAlignment="Top"/>
                            </Border>
                            <TextBlock Margin="92,68,80,81" TextWrapping="Wrap" Text="" RenderTransformOrigin="0.265,0.51" HorizontalAlignment="Center" Width="271" x:Name="programName"/>
                            <TextBlock Height="32" Margin="21,0,16,12" TextWrapping="Wrap" Text="Date:" VerticalAlignment="Bottom" x:Name="CurrentDate"/>
                        </Grid>
                    </ControlTemplate>

我显示按钮的代码是这样的:

    <Grid x:Name="middleRow" Grid.Row="2">
        <Button Content="Button" Margin="8,8,0,8" Style="{StaticResource ButtonCenter}" x:Name="Current" Click="Current_Click" d:LayoutOverrides="GridBox" />
    </Grid>

在我的代码后面的 InitializeComponent(); 之后我想更改 ProgramName 文本块和 CurrentDate 文本块。

我想我可能必须创建一个控件来执行此操作,但我不确定。我的尝试失败了(杂项错误)。我可以在代码中访问这些文本块吗?请告诉我。

更新:

我最终这样做了:

<Button Margin="8,8,0,8" x:Name="Current" Click="Current_Click">  
    <Button.Content>  
        <StackPanel>  
            <TextBlock x:Name="ProgramName" Text="program name" HorizontalAlignment="Center" />  
            <TextBlock x:Name="CurrentDate" Text="current date" HorizontalAlignment="Center" />  
        </StackPanel>  
    </Button.Content>  
</Button> 

我在 Blend 中应用了模板中的样式,它现在似乎正在工作。

I have created a button in Blend by editing it's style. I added multiple text blocks with the intention of displaying data to the user in real time. However, I don't know how to interface with those text blocks in my code behind.

My Style XAML is this:

<ControlTemplate TargetType="Button">
                        <Grid Background="Transparent">
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="CommonStates">
                                    <VisualState x:Name="Normal"/>
                                    <VisualState x:Name="MouseOver"/>
                                    <VisualState x:Name="Pressed"/>
                                    <VisualState x:Name="Disabled"/>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                            <Border x:Name="ButtonBackground" BorderThickness="{TemplateBinding BorderThickness}" Background="{StaticResource PhoneAccentBrush}" CornerRadius="0" Margin="8,12,12,12">
                                <TextBlock Margin="121,5,98,0" TextWrapping="Wrap" Text="Current Program:" Height="36" VerticalAlignment="Top"/>
                            </Border>
                            <TextBlock Margin="92,68,80,81" TextWrapping="Wrap" Text="" RenderTransformOrigin="0.265,0.51" HorizontalAlignment="Center" Width="271" x:Name="programName"/>
                            <TextBlock Height="32" Margin="21,0,16,12" TextWrapping="Wrap" Text="Date:" VerticalAlignment="Bottom" x:Name="CurrentDate"/>
                        </Grid>
                    </ControlTemplate>

My code to display the button is this:

    <Grid x:Name="middleRow" Grid.Row="2">
        <Button Content="Button" Margin="8,8,0,8" Style="{StaticResource ButtonCenter}" x:Name="Current" Click="Current_Click" d:LayoutOverrides="GridBox" />
    </Grid>

In my code behind after the InitializeComponent(); I would like to change the ProgramName text block and the CurrentDate text block.

I'm thinking that I might have to create a control to do this but I'm not sure. My attempts at doing so failed (misc. errors). Can I access these text blocks in code? Please let me know.

UPDATE:

I wound up doing it like this:

<Button Margin="8,8,0,8" x:Name="Current" Click="Current_Click">  
    <Button.Content>  
        <StackPanel>  
            <TextBlock x:Name="ProgramName" Text="program name" HorizontalAlignment="Center" />  
            <TextBlock x:Name="CurrentDate" Text="current date" HorizontalAlignment="Center" />  
        </StackPanel>  
    </Button.Content>  
</Button> 

I applied my styles from the template in Blend and it appears to be working now.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅暮の光 2024-10-14 02:46:45

您不能按名称引用样式中的控件,因为页面上可能有它们的多个副本。

如果您将按钮制作为自定义控件,则可以为 ProgramTitle 和 CurrentDate 属性制作文本(这将非常容易设置)。

或者,您可以使用数据绑定来设置这些值。

You can't reference controls in a style by name as there could be multiple copies of them on a page.

If you made your button into a custom control you could make the text for the ProgramTitle and CurrentDate properties (which woudl be very easy to set).

Alternatively you could use databinding to set these values.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文