如何从DataTemplate获取代码中的元素 [英] How to get element in code behind from DataTemplate

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

问题描述

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

I have a FlipView control with the DataTemplate defined as below:

<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.这是我的代码:

In my code behind, I need to have access to the TextBlock named "xxxTB". Here is my code to do that:

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) 不返回任何内容.

But, when it finds the FlipView in the VisualTree, it returns from: for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++) because VisualTreeHelper.GetChildrenCount(depObj) does not return anything.

有什么想法吗?

推荐答案

这里有一个可行的解决方案:

So here is a working solution:

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 中使用此技巧.

Now, I am able to load the control at least. (However, I read that this trick should not be used in the overridden method OnApplyTemplate for some reason).

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

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