NavigateUrl绑定和格式化问题

时间:2017-06-02 20:08:01

标签: wpf data-binding

我有

<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)?

对于测试块文本,绑定和格式按预期工作

1 个答案:

答案 0 :(得分:0)

好的,以这种方式更改了我的扩展程序:

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>