【问题标题】:Get the controls inside DataTemplate control获取 DataTemplate 控件内的控件
【发布时间】:2014-10-08 13:46:02
【问题描述】:

我有以下 XAML 代码用于 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>

问题是我无法访问 c# 页面中的 MapLayerGrid 控件。 (仅当 XAML 位于 DataTepmlate 控件内时才会出现问题)。 我怎样才能获得此访问权限?

【问题讨论】:

  • 为您的 Grid 添加一个名称并使用 Control.Findname 方法获取您的子控件
  • @Sajeetharan 感谢您的评论,但您能否发布一个示例以说明代码的外观? (当我添加 name 属性时出现错误:“属性名称设置不止一次)”
  • 我昨天回答了这个问题:stackoverflow.com/questions/26236707/…(只是另一个示例..)
  • &lt;m:MapLayer Name="DataLayer" Loaded="yourloadedevent" /&gt; 并在处理程序中使用您的代码。请在此处发布您的代码,以便其他人可以查看。

标签: c# xaml datatemplate


【解决方案1】:

您应该使用 VisualTreeHelper 方法。这只是我正在使用的一些代码。我认为您可以根据需要轻松调整它。

首先将 FindElementByName 方法放入代码隐藏文件中的某个位置:

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 或您的地图,如下所示:

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

在您的处理程序中,您现在可以使用这样的代码访问元素(您可能需要调整它,因为我对 Hubsection 控件不太熟悉):

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.
*/

【讨论】:

    猜你喜欢
    • 2012-01-04
    • 2011-05-13
    • 1970-01-01
    • 2011-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-15
    • 1970-01-01
    相关资源
    最近更新 更多