hewstone
hewstone

Reputation: 4695

Silverlight: Get Element in DataTemplate by Name

I am trying to bind to a HyperlinkButton inside a DataTemplate by name after the users sets a dependancy property. (All code is in Silverlight 4) I unforunately don't know the field to bind to until runtime. I know I can create the DataTemplate at runtime as a string with the correct binding path and inject it as XmlReader but this feels hacky. The error I continue to get from the FindVisualChild function is "Reference is not a valid visual DependencyObject". How can I get a reference to the HyperlinkButton from within the datatemplate so I can set the bindings?

Here is my code that I am working with:

XAML:

 <sdk:DataGridTemplateColumn x:Class="CHK.WebMap.SL.Controls.DataGridURLTemplateColumn"
    xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">
    <sdk:DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <HyperlinkButton x:Name="btnHyperlink" TargetName="_blank" />
        </DataTemplate>
    </sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>

CodeBehind:

public partial class DataGridURLTemplateColumn : DataGridTemplateColumn
{
    public string NavigateUri
    {
        get { return (string)GetValue(NavigateUriProperty); }
        set { SetValue(NavigateUriProperty, value); }
    }
    public static readonly DependencyProperty NavigateUriProperty =
        DependencyProperty.Register("NavigateUri", typeof(string), typeof(DataGridURLTemplateColumn), new PropertyMetadata((s, e) =>
        {
            var context = s as DataGridURLTemplateColumn;
            context.CellTemplate.LoadContent(); //create the ui elements

            var hyperlinkButton = context.FindVisualChild<HyperlinkButton>(context) as HyperlinkButton;
            hyperlinkButton.SetBinding(HyperlinkButton.NavigateUriProperty, new Binding(e.NewValue as string));
            hyperlinkButton.SetBinding(HyperlinkButton.ContentProperty, new Binding(e.NewValue as string));
        }));

    public DataGridURLTemplateColumn()
    {
        InitializeComponent();
    }


    private childItem FindVisualChild<childItem>(DependencyObject obj) where childItem : DependencyObject
    {

        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
        {

            DependencyObject child = VisualTreeHelper.GetChild(obj, i);

            if (child != null && child is childItem)
                return (childItem)child;
            else
            {
                childItem childOfChild = FindVisualChild<childItem>(child);
                if (childOfChild != null)
                    return childOfChild;
            }
        }

        return null;
    }
}

Upvotes: 2

Views: 8163

Answers (1)

hewstone
hewstone

Reputation: 4695

Okay I figured out the answer. To my question of how to get elements inside a DataTemplate. I actually had it in the code sample but was not saving the results to a variable.

var hyperlinkButton = context.CellTemplate.LoadContent() as HyperlinkButton; 

Upvotes: 5

Related Questions