简体   繁体   English

访问代码隐藏的Silverlight控件模板项

[英]Accessing Silverlight Control template items codebehind

I have a control template in page Layout as following. 我在页面布局中有一个控件模板,如下所示。

<Grid x:Name="LayoutRoot">
        <Grid.Resources>
        <ControlTemplate x:Key="myTemplate" TargetType="esri:MapTip">
                <Border CornerRadius="10" Background="#DDFFEEEE" BorderThickness="4" BorderBrush="#99FF0000">
                    <StackPanel Background="#DDFFFFFF">

                      <sdk:TabControl Height="180" Margin="5"  Name="tabControl1"  Width="300">      
                               <sdk:TabItem Header="Info" Name="infoTab">

                               <TextBlock x:Name="cityInfoTxt" Tag="{Binding [City_ID]}"/>    

                               </sdk:TabItem>
                        </sdk:TabControl>
                    </StackPanel>
                </Border>
            </ControlTemplate>

In code behind how can I access the ?? 在后面的代码中我该如何访问?

I tried this, 我试过了

private void button1_Click(object sender, RoutedEventArgs e)
{
    var te = this.LayoutRoot.Resources["myTemplate"] as ControlTemplate;

}

but can not access the textblock in the tab control. 但无法访问标签控件中的文本块。

override the method OnAplyTemplate() on code behind and try to find your component. 在后面的代码上重写方法OnAplyTemplate()并尝试查找您的组件。

    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();
        var myTextBlock = GetTemplateChild("cityInfoTxt") as TextBlock;
    }

Hope it helps 希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM