Wpf NavigateUrl绑定和格式问题

Wpf NavigateUrl绑定和格式问题,wpf,data-binding,Wpf,Data Binding,我有 当我试图从e获取url时,我只有一个绑定名称部分,它位于{0}中。 如何获取格式化的文件() 对于测试块文本,绑定和格式化工作正常好的,将我的扩展名更改为: public class HyperlinkExtensions { public static bool GetIsExternal(DependencyObject obj) { return (bool)obj.GetValue(IsExternalProperty); } pu

我有

当我试图从e获取url时,我只有一个绑定名称部分,它位于{0}中。 如何获取格式化的文件()


对于测试块文本,绑定和格式化工作正常

好的,将我的扩展名更改为:

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;
    }
}
用法:

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


StringFormat仅在绑定的目标属性为string类型时使用,而NavigateUri不是。请改用绑定转换器。@Clements谢谢!
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>