2017-06-02 58 views
0

我有NavigateUrl绑定和格式问题

<Hyperlink attachedProperty:HyperlinkExtensions.IsExternal="true"> 
    <Hyperlink.NavigateUri> 
     <Binding Path="Name" StringFormat="http://anycoolsite.net/tag={0}" /> 
    </Hyperlink.NavigateUri> 
    <TextBlock> 
      <TextBlock.Text> 
       <Binding Path="Title" StringFormat="http://anycoolsite.net/tag={0}" /> 
      </TextBlock.Text> 
     </TextBlock> 
</Hyperlink> 

和该附加属性

public class HyperlinkExtensions 
{ 
    public static bool GetIsExternal(DependencyObject obj) 
    { 
     return (bool)obj.GetValue(IsExternalProperty); 
    } 

    public static void SetIsExternal(DependencyObject obj, bool value) 
    { 
     obj.SetValue(IsExternalProperty, value); 
    } 
    public static readonly DependencyProperty IsExternalProperty = 
     DependencyProperty.RegisterAttached("IsExternal", typeof(bool), typeof(HyperlinkExtensions), new UIPropertyMetadata(false, OnIsExternalChanged)); 

    private static void OnIsExternalChanged(object sender, DependencyPropertyChangedEventArgs args) 
    { 
     var hyperlink = sender as Hyperlink; 

     if ((bool)args.NewValue) 
      hyperlink.RequestNavigate += Hyperlink_RequestNavigate; 
     else 
      hyperlink.RequestNavigate -= Hyperlink_RequestNavigate; 
    } 

    private static void Hyperlink_RequestNavigate(object sender, System.Windows.Navigation.RequestNavigateEventArgs e) 
    { 
     Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri)); 
     e.Handled = true; 
    } 
} 

当我试图从E中的网址,我只有一个约束名称部分,这是在{0}。 如何获得格式化的(http://anycoolsite.net/anyboundname)?

对于testblock文本的结合和格式按预期工作

+1

的StringFormat仅用于当绑定的目标属性是字符串类型,NavigateUri不是。改为使用绑定转换器。 – Clemens

+0

@Clements谢谢你! – amplifier

回答

0

OK,改变了我的扩展是这样的:

public class HyperlinkExtensions 
{ 
    public static string GetUrlFormat(DependencyObject obj) 
    { 
     return (string)obj.GetValue(UrlFormatProperty); 
    } 

    public static void SetUrlFormat(DependencyObject obj, string value) 
    { 
     obj.SetValue(UrlFormatProperty, value); 
    } 

    public static readonly DependencyProperty UrlFormatProperty = 
     DependencyProperty.RegisterAttached("UrlFormat", typeof(string), typeof(HyperlinkExtensions), new UIPropertyMetadata(string.Empty, OnUrlFormatChanged)); 

    private static void OnUrlFormatChanged(object sender, DependencyPropertyChangedEventArgs args) 
    { 
     var hyperlink = sender as Hyperlink; 
     if (!string.IsNullOrEmpty((string) args.NewValue)) 
     { 
      hyperlink.NavigateUri = new Uri(string.Format((string) args.NewValue, hyperlink.NavigateUri)); 
      hyperlink.RequestNavigate -= Hyperlink_RequestNavigate; 
      hyperlink.RequestNavigate += Hyperlink_RequestNavigate; 
     } 
     else 
     { 
      hyperlink.RequestNavigate -= Hyperlink_RequestNavigate; 
     } 
    } 


    private static void Hyperlink_RequestNavigate(object sender, System.Windows.Navigation.RequestNavigateEventArgs e) 
    { 
     Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri)); 
     e.Handled = true; 
    } 
} 

用法:

<Hyperlink attachedProperty:HyperlinkExtensions.UrlFormat="https://anycoolsite.com/products/{0}" ToolTip="Click to open in browser"> 
    <Hyperlink.NavigateUri> 
     <Binding Path="ProductName"/> 
    </Hyperlink.NavigateUri> 
    <TextBlock Text="{Binding Title}"/> 
</Hyperlink>