LinkButton doesn't work Silverlight

本文关键字:work Silverlight doesn LinkButton | 更新日期: 2023-09-27 18:06:51

我调用了一个方法来创建一个链接按钮。然而,问题是,它传递的字段名称,实际字段链接按钮,因为给我一个死链接。

方法:

     private static DataGridTemplateColumn CreateHyperlink(string fieldName)
    {
        DataGridTemplateColumn column = new DataGridTemplateColumn();
        column.Header = "";
        string link = @"http://www.amazon.com/gp/product/" + fieldName;
        StringBuilder sb = new StringBuilder();
        sb.Append("<DataTemplate ");
        sb.Append("xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' ");
        sb.Append("xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' ");
        sb.Append("xmlns:src='clr-namespace:Silverlight1.Classes;assembly=Silverlight1.Classes'>");
        sb.Append("<HyperlinkButton ");
        sb.Append("TargetName= '_blank' ");
        sb.Append("Content = 'Link' ");
        sb.Append("NavigateUri =" +"'"+ link +"'");
        sb.Append(" />");
        sb.Append("</DataTemplate>");
        column.CellTemplate = (DataTemplate)XamlReader.Load(sb.ToString());
        column.IsReadOnly = false;
        return column;
    }

被这个

调用

dgOrder.Columns.Add(CreateHyperlink("asin"));

是从WCF Silverlight启用数据服务中提取的。如何传递内容而不是字段名称?

LinkButton doesn't work Silverlight

您不传递内容,您传递字段名称,但创建Binding。您还需要一个IValueConverter的实现。

让我们从IValueConverter开始,你需要一些东西,它采取"字段"的字符串值(我假设你的意思是绑定到网格行的对象的属性),并以@"http://www.amazon.com/gp/product/"为前缀,形成一个完整的URL。

public class UrlPrefixConverter : IValueConverter
{
     public string Prefix {get; set;}
     public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value != null)
        { 
            return new Uri(Prefix + value.ToString(), UriKind.Absolute);
        }
        return null;
    }
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

在App.xaml:-

 <Application.Resources>
      <local:UrlPrefixConverter Prefix="http://www.amazon.com/gp/product/" x:Key="AmazonPrefixConverter" />
 </Application.Resources>
现在你的create方法看起来像这样:-
    private static DataGridTemplateColumn CreateHyperlink(string fieldName)
    {
        DataGridTemplateColumn column = new DataGridTemplateColumn();
        column.Header = "";
        string template = @"<DataTemplate
                xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" >
                <HyperlinkButton TargetName=""_blank"" Content=""Link""
                     NavigateUri=""{{Binding {0}, Converter={{StaticResource AmazonPrefixConverter}}}}"" />
             </DataTemplate>"; 
        column.CellTemplate = (DataTemplate)XamlReader.Load(String.Format(template, fieldName));
        column.IsReadOnly = false;
        return column;
    }