How to get element in code behind from DataTemplate
Asked Answered
A

3

6

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>

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

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.

Any idea?

Aluminum answered 21/12, 2015 at 12:43 Comment(3)
Use GetTemplateChild("xxxTB").Buttocks
I tried, but it returns null.Aluminum
See it this wont help: wpftutorial.net/DataTemplates.htmlStepdame
A
6

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).

Aluminum answered 24/12, 2015 at 11:54 Comment(0)
O
2

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;
            }
Orndorff answered 22/12, 2015 at 11:30 Comment(1)
I get dt.FindName API does not exist.Aluminum
E
1

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;
    }
Enemy answered 22/12, 2015 at 12:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.