获取的DataTemplate控件中 [英] Get the controls inside DataTemplate control

查看:172
本文介绍了获取的DataTemplate控件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在集线器应用中为Windows 8.1以下XAML代码:

I have the following XAML code that used on hub application for windows 8.1:

<HubSection Width="780" Margin="0,0,80,0">
                <HubSection.Background>
                    <ImageBrush ImageSource="Assets/MediumGray.png" Stretch="UniformToFill" />
                </HubSection.Background>
                <DataTemplate>
                    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
                        <m:Map Credentials="YOUR_BING_MAPS_KEY">
                            <m:Map.Children>
                                <!-- Data Layer-->
                                <m:MapLayer Name="DataLayer"/>

                                <!--Common Infobox-->
                                <m:MapLayer>
                                    <Grid x:Name="Infobox" Visibility="Collapsed" Margin="0,-115,-15,0">
                                        <Border Width="300" Height="110" Background="Black" Opacity="0.8" BorderBrush="White" BorderThickness="2" CornerRadius="5"/>

                                    </Grid>
                                </m:MapLayer>
                            </m:Map.Children>
                        </m:Map>
                    </Grid>

                </DataTemplate>
            </HubSection>



现在的问题是,我不能访问 MapLayer ,并在C#页电网控制。
(这个问题只发生在磨片XAML是 DataTepmlate 控件内)。
我怎样才能得到这个访问?

The problem is that I can't access to MapLayer and to the Grid controls in the c# page. (The problem happens only whe the XAML is inside the DataTepmlate control). How can I get this access?

推荐答案

您应该使用VisualTreeHelper方法。这仅仅是一些代码,我使用。我想你可以很容易地调整到您的需要。

You should use a VisualTreeHelper method. This is just some code I am using. I think you can easily adjust it to your needs.

先放FindElementByName方法的地方到你的代码隐藏文件:

First put the FindElementByName method somewhere into your code behind file:

public T FindElementByName<T>(DependencyObject element, string sChildName) where T : FrameworkElement
    {
        T childElement = null;
        var nChildCount = VisualTreeHelper.GetChildrenCount(element);
        for (int i = 0; i < nChildCount; i++)
        {
            FrameworkElement child = VisualTreeHelper.GetChild(element, i) as FrameworkElement;

            if (child == null)
                continue;

            if (child is T && child.Name.Equals(sChildName))
            {
                childElement = (T)child;
                break;
            }

            childElement = FindElementByName<T>(child, sChildName);

            if (childElement != null)
                break;
        }
        return childElement;
    }

现在你可以开始使用方法。事件处理程序添加到您的MapLayer或您的地图是这样的:

Now you can start using the method. Add an event handler to your MapLayer or to your Map like this:

<m:MapLayer Name="DataLayer" Loaded="DataLayerLoaded" />



内部处理程序,你现在就可以使用如下代码访问该元素(您可能需要调整此作为我不是太熟悉Hubsection控制):

Inside your handler you can now access the element with code like this (you might have to adjust this as I am not too familiar with the Hubsection control):

this.UpdateLayout();
// Give your hub a name using x:Name=
var item = [..] // Retrieve your hubsection here!
var container = this.MyHubSection.ContainerFromItem(item);
// NPE safety, deny first
if (container == null)
    return;
var datalayer = FindElementByName<MapLayer>(container, "DataLayer");
// And again deny if we got null
if (datalayer == null)
    return;
/*
  Start doing your stuff here.
*/

这篇关于获取的DataTemplate控件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆