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

标签 silverlight dynamic binding datatemplate

在用户设置依赖属性后,我尝试按名称绑定(bind)到 DataTemplate 内的 HyperlinkBut​​ton。 (所有代码都在 Silverlight 4 中)不幸的是,直到运行时我才知道要绑定(bind)到的字段。我知道我可以在运行时将 DataTemplate 创建为具有正确绑定(bind)路径的字符串,并将其注入(inject)为 XmlReader,但这感觉很奇怪。我继续从 FindVisualChild 函数得到的错误是“引用不是有效的视觉 DependencyObject”。如何从数据模板中获取对 HyperlinkBut​​ton 的引用,以便设置绑定(bind)?

这是我正在使用的代码:

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>

隐藏代码:

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

最佳答案

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

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

关于Silverlight:按名称获取 DataTemplate 中的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6293357/

相关文章:

c# - 将 anchor 线添加到 Bing map 自定义图钉并允许用户拖动

wpf - 程序员编辑 XAML 的最佳方式

c# - 从字符串加载图像

c# - WPF View 谁导致另一个使用 MVVM

c# - 如何在 ObservableCollection 中捕获通用对象的类型?

arrays - Pharo 3 中文字数组和动态数组的区别

python - 动态路由返回 favicon.ico 而不是 Flask 中的变量

javascript - 使用 JavaScript 的动态 div

java - GWT 问题,GWT.create(SomeClass.class) 抛出异常

c# - RelayCommand 未执行