Silverlight:按名称在DataTemplate中获取元素

hew*_*one 2 silverlight binding dynamic datatemplate

我试图在用户设置依赖属性后,按名称绑定到DataTemplate中的HyperlinkBut​​ton.(所有代码都在Silverlight 4中)我还是不知道要绑定到运行时的字段.我知道我可以在运行时创建DataTemplate作为一个具有正确绑定路径的字符串,并将其注入XmlReader,但这感觉很hacky.我继续从FindVisualChild函数获得的错误是"引用不是有效的可视化DependencyObject".如何从datatemplate中获取对HyperlinkBut​​ton的引用,以便我可以设置绑定?

这是我正在使用的代码:

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>
Run Code Online (Sandbox Code Playgroud)

代码隐藏:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

hew*_*one 5

好的,我想出了答案.关于如何在DataTemplate中获取元素的问题.我实际上在代码示例中有它,但没有将结果保存到变量.

var hyperlinkButton = context.CellTemplate.LoadContent() as HyperlinkButton; 
Run Code Online (Sandbox Code Playgroud)