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.
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.
精彩评论