简体   繁体   English

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

[英]How to get element in code behind from DataTemplate

I have a FlipView control with the DataTemplate defined as below:我有一个 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>

In my code behind, I need to have access to the TextBlock named "xxxTB".在我后面的代码中,我需要访问名为“xxxTB”的 TextBlock。 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")
            { }                    
        }
}

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

Any idea?任何的想法?

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")
        { }
    }
}

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). (但是,我读到由于某种原因,不应在重写的方法 OnApplyTemplate 中使用此技巧)。

Try this尝试这个

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

You can try this:你可以试试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM