简体   繁体   English

为什么按钮的内容模板没有对象?

[英]why content template of button does not have object?

i am just exploring windows phone runtime apps template . 我只是在探索Windows Phone运行时应用程序模板 But i am seeing a weird thing. 但是我看到一个奇怪的事情。

I have Button defined in Xaml with ContentTemplate set in it. 我在Xaml中定义了Button,并在其中设置了ContentTemplate I wanted extract the Image control defined in the ContentTemplate of this button. 我想提取此按钮的ContentTemplate中定义的Image控件。 But it is coming null. 但这是空的。

Xaml code :- XAML代码 :-

<Button x:Name="PlayButton" Click="PlayButton_OnClick">
        <Button.ContentTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal" Name="Panel">
                    <Image x:Name="ControlImg" 
                     Width="100"
                    />
                    <TextBlock Text="text block" />
                </StackPanel>
            </DataTemplate>
        </Button.ContentTemplate>

Here is button Click event :- 这是按钮单击事件 :-

private async void PlayButton_OnClick(object sender, RoutedEventArgs e)
{
        var btn = sender as Button;

        var ct = btn.ContentTemplate; // this part is also not showing controls in it when expending ct at runtime.

        var img = btn.FindName("ControlImg") as Image; // coming null 

        var stckpnl = btn.FindName("Panel") as StackPanel;// coming null
}

Can anybody check this out why is this happening ? 有人可以检查出这是为什么吗?

Edit :- I have broken my problem and reach this very bottom simple level and after seeing this i am just not getting why is this happening ? 编辑 :-我已经解决了我的问题,并达到了这个非常简单的底层,看到这个之后,我只是不明白为什么会这样?

That is strange behavior. 那是奇怪的行为。 It should have stack Panel and image in control template. 它应该在控制模板中具有堆栈面板和图像。 As a work around you can use ContentTemplateRoot to get the image and stackpanel. 作为解决方法,您可以使用ContentTemplateRoot获取图像和堆栈面板。 I have test this, it is working. 我已经测试过了,它正在工作。

((StackPanel)btn.ContentTemplateRoot).Children[0] // image

Hope this helps 希望这可以帮助

Edit: 编辑:

For Details about why FindName is not working see the the Remarks section on on MSDN . 有关FindName为什么不起作用的详细信息,请参见MSDN上的“备注”部分。 Here is some relevant quotes 这是一些相关的报价

Important In order to use the FindName method effectively, you should understand the concept of a XAML namescope, and how a XAML namescope is created at XAML load time and then referenced and possibly modified at run time. 重要说明为了有效地使用FindName方法,您应该了解XAML名称范围的概念,以及如何在XAML加载时创建XAML名称范围,然后在运行时对其进行引用和可能的修改。 For more info see XAML namescopes. 有关更多信息,请参见XAML名称范围。 The most common usage of FindName in your Windows Runtime code will be from within the generated InitializeComponent call for a XAML page. Windows运行时代码中最常见的FindName用法是从XAML页面的InitializeComponent调用中产生。 In this situation, FindName is invoked only after the XAML page is loaded. 在这种情况下,仅在加载XAML页面之后才调用FindName。 InitializeComponent provides the infrastructure such that any object that was instantiated by XAML loading can conveniently be accessed by your code-behind code. InitializeComponent提供了基础结构,以便您的代码隐藏代码可以方便地访问XAML加载实例化的任何对象。 You can then reference the objects as a variable that shares the same name as the markup-declared x:Name. 然后,您可以将对象引用为变量,该变量与标记声明的x:Name具有相同的名称。 A run-time API such as FindName is working against a run-time object tree of the app as it exists in memory. 像FindName这样的运行时API正在内存中存在的应用程序的运行时对象树上工作。 When part of this object tree is created from templates or run-time loaded XAML, a XAML namescope is typically not contiguous within that object tree. 当从模板或运行时加载的XAML创建该对象树的一部分时,XAML名称范围通常在该对象树中不连续。 The result is that there might be a named object in the object tree that a given FindName scope cannot find. 结果是在对象树中可能存在一个给定的FindName范围找不到的命名对象。 The discontinuities between XAML namescopes that you might encounter in typical application scenarios are when objects are created by applying a template, or when objects are created by a call to XamlReader.Load and subsequently added to the object tree. 在典型的应用程序场景中,您可能会遇到XAML名称范围之间的不连续性,这是通过应用模板来创建对象时,或者是通过调用XamlReader.Load创建对象并随后将其添加到对象树中时。

As you are using DataTemplate so the xaml object tree is not contiguous so that is why FindName is failed to find the control from the xaml tree. 当您使用DataTemplate时,xaml对象树不是连续的,这就是为什么FindName无法从xaml树中找到控件的原因。

hope this explains... 希望这能解释...

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

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