当前位置: 动力学知识库 > 问答 > 编程问答 >

silverlight - Get button in Pivot Header programatically in Windows Phone 8

问题描述:

Summary: I need to retrieve a button (in order to attach a click handler) from a pivot of which template is defined as static resource. How can I do this?

A couple of views contain a pivot control, and style of these pivots are defined in App.xaml as resource.

Here is XAML for Pivot in multiple pages:

<phone:Pivot Title="Onaylar" Name="pivot" Style="{StaticResource MyPivotStyle}">

<phone:PivotItem Header="first pivot item">

<TextBlock Foreground="{StaticResource NavyTextColor}" Margin="10,0,0,0" Text="first pivot item"></TextBlock>

</phone:PivotItem>

<phone:PivotItem Header="second pivot item">

<TextBlock Foreground="{StaticResource NavyTextColor}" Margin="10,0,0,0" Text="second pivot item"></TextBlock>

</phone:PivotItem>

<phone:PivotItem Header="third pivot item">

<TextBlock Foreground="{StaticResource NavyTextColor}" Margin="10,0,0,0" Text="third pivot item"></TextBlock>

</phone:PivotItem>

</phone:Pivot>

Here is the style for pivot defined in App.xaml (shortened for convenience):

<Style x:Key="MyPivotStyle" TargetType="phone:Pivot">

<Setter Property="Template">

<Setter.Value>

<ControlTemplate TargetType="phone:Pivot">

<Grid>

<Grid.RowDefinitions>

<RowDefinition Height="Auto"/>

<RowDefinition Height="Auto"/>

<RowDefinition Height="*"/>

</Grid.RowDefinitions>

<Grid CacheMode="BitmapCache" Grid.RowSpan="2">

<Grid.Background>

<ImageBrush ImageSource="/Assets/bg_header.png"/>

</Grid.Background>

</Grid>

<Grid Background="{TemplateBinding Background}" CacheMode="BitmapCache" Grid.Row="2" />

<ContentPresenter ContentTemplate="{TemplateBinding TitleTemplate}" Margin="24,17,0,-7">

<StackPanel Orientation="Horizontal">

<Image Source="/Assets/company_name.png" Width="213.75" HorizontalAlignment="Left" VerticalAlignment="Top" />

<Button HorizontalAlignment="Right" VerticalAlignment="Top" Margin="140,-20,0,35" BorderThickness="0" x:Name="btnHome">

<Image Source="/Assets/btnHome.png" Width="48" Height="48" ></Image>

</Button>

</StackPanel>

</ContentPresenter>

<controlsPrimitives:PivotHeadersControl x:Name="HeadersListElement" Foreground="White" Grid.Row="1"/>

<ItemsPresenter x:Name="PivotItemPresenter" Margin="{TemplateBinding Padding}" Grid.Row="2"/>

</Grid>

</ControlTemplate>

</Setter.Value>

</Setter>

</Style>

What I want to do is this:

var myButton = GetButtonWithName("btnHome") as Button;

myButton.Click += Handler;

However, I cannot get the button. Any solutions?

网友答案:

i think the main proplem is how to find control in template .

so i think this link can help you :

How to get Control from DataTemplate to ControlTemplate ?

分享给朋友:
您可能感兴趣的文章:
随机阅读: