0
<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文本的结合和格式按预期工作
的StringFormat仅用于当绑定的目标属性是字符串类型,NavigateUri不是。改为使用绑定转换器。 – Clemens
@Clements谢谢你! – amplifier