如何从DataTemplate中获取代码中的元素

时间:2015-12-21 12:43:18

标签: c# wpf datatemplate itemtemplate

我有一个FlipView控件,其DataTemplate定义如下:

<FlipView x:Name="FlipView5Horizontal" Width="480" Height="270" BorderBrush="Black" BorderThickness="1" Style="{StaticResource FlipViewStyle1}">
        <FlipView.ItemTemplate>
          <DataTemplate>
            <Grid>
              <Image Width="480" Name="xxxImage" Height="270" Source="{Binding Image}" Stretch="UniformToFill"/>
              <Border Name="xxxBorder" Background="#A5000000" Height="80" VerticalAlignment="Bottom">
                <TextBlock Name="xxxTB" Text="{Binding Title}" FontFamily="Segoe UI" FontSize="26.667" Foreground="#CCFFFFFF" Padding="15,20"/>
              </Border>
            </Grid>
          </DataTemplate>
        </FlipView.ItemTemplate>
      </FlipView>

在我的代码中,我需要访问名为&#34; xxxTB&#34;的TextBlock。这是我的代码:

public IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
        {
            if (depObj != null)
            {
                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
                {
                    DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
                    if (child != null && child is T)
                    {
                        yield return (T)child;
                    }

                    foreach (T childOfChild in FindVisualChildren<T>(child))
                    {
                        yield return childOfChild;
                    }
                }
            }
        }

public void TestMethod()
{
        foreach (var item in FindVisualChildren<TextBlock>(this))
        {
            if (timeLine.Name == "xxxTB")
            { }                    
        }
}

但是,当它在VisualTree中找到FlipView时,它会从:for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)返回,因为VisualTreeHelper.GetChildrenCount(depObj)不会返回任何内容。

有什么想法吗?

3 个答案:

答案 0 :(得分:2)

试试这个

 ContentPresenter cp = GetFrameworkElementByName<ContentPresenter>(FlipView5Horizontal);
                DataTemplate dt = FlipView5Horizontal.ItemTemplate;
                TextBlock l = (dt.FindName("xxxTB", cp)) as TextBlock;  




 private static T GetFrameworkElementByName<T>(FrameworkElement referenceElement) where T : FrameworkElement
            {
                FrameworkElement child = null;
                for (Int32 i = 0; i < VisualTreeHelper.GetChildrenCount(referenceElement); i++)
                {
                    child = VisualTreeHelper.GetChild(referenceElement, i) as FrameworkElement;
                    System.Diagnostics.Debug.WriteLine(child);
                    if (child != null && child.GetType() == typeof(T))
                    {
                        break;
                    }
                    else if (child != null)
                    {
                        child = GetFrameworkElementByName<T>(child);
                        if (child != null && child.GetType() == typeof(T))
                        {
                            break;
                        }
                    }
                }
                return child as T;
            }

答案 1 :(得分:2)

所以这是一个有效的解决方案:

public void TestMethod()
{
    DataTemplate dt = FlipView5Horizontal.ItemTemplate;
    DependencyObject dio = dt.LoadContent();
    foreach (var timeLine in FindVisualChildren<TextBlock>(dio)) //FindVisualTree is defined in the question :)
    {
        if (timeLine.Name == "xxxTB")
        { }
    }
}

现在,我至少能够加载控件。 (但是,我读到由于某些原因,不应该在重写的方法OnApplyTemplate中使用此技巧。)

答案 2 :(得分:1)

你可以试试这个:

 var textblock = IteratingKeyboardChildren(g, keyName);  

Border IteratingKeyboardChildren(Grid g, string keyName)
    {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(g); i++)
        {
            var child = VisualTreeHelper.GetChild(g, i);
            if (child is TextBlock )
            {
                if ((child as TextBlock ).Tag.ToString().Equals( keyName))
                    return child as TextBlock ;
            }
        }
        return null;
    }