如何在后端代码中从DataTemplate获取元素

6

我有一个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>

在我的代码后台,我需要访问名为“xxxTB”的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)没有返回任何内容。

有什么想法吗?


使用 GetTemplateChild("xxxTB") - Fᴀʀʜᴀɴ Aɴᴀᴍ
我尝试了,但它返回 null。 - tavier
这个不会有帮助:http://wpftutorial.net/DataTemplates.html - Bayeni
3个回答

6

以下是一个可行的解决方案:

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

试试这个

 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;
            }

我得到了 dt.FindName API 不存在。 - tavier

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;
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接