這是我想要實現的第一個版本。肯定有錯誤在這裏,我會盡量更新這個答案。
與下面描述的方法,我現在可以寫這樣的XAML代碼和快樂:)
<popupControl:HyperlinkPopup Margin="2, 0, 0, 0" HyperlinkText="{x:Static properties:Resources.UpdateIsAvailableText}">
<popupControl:HyperlinkPopup.Popup>
<TextBlock Margin="10" TextWrapping="Wrap" MaxWidth="600">
<Run Text="{Binding Path=UpdateMessage, Mode=OneWay}"></Run>
<Hyperlink Click="InstallClick"><Run Text="{x:Static properties:Resources.InstallText}"/></Hyperlink>
</TextBlock>
</popupControl:HyperlinkPopup.Popup>
</popupControl:HyperlinkPopup>
這似乎正是我需要爲我的應用程序:
- 有能力提供一個文本對於超鏈接
- 指定我對此特定超鏈接的自定義彈出框(對於不同的超鏈接可以不同 - 這是整個想法)
- 沒有代碼在這裏我使用這個控制
總體地方:
- 具有兩個屬性(彈出和HyperlinkText)
- 在Generic.xaml
- 定義的OnClick定義XAML(可通過迭代通過執行定義的自定義控制UI元素 - 不確定是否更容易做到這一點)
缺點:
- 由於某些原因,VS和Blend都只能在第一次打開VS/Blend時可視化這個控件。一旦我編譯它第二次顯示爲空
- 不知道這是否是實現這樣的事情的最佳方式。任何想法如何改善?
實現:
我定義的自定義控制:
/// <summary>The hyperlink popup.</summary>
public class HyperlinkPopup : ContentControl
{
#region Constants and Fields
/// <summary>The popup property.</summary>
public static readonly DependencyProperty PopupProperty = DependencyProperty.Register(
"Popup",
typeof(object),
typeof(HyperlinkPopup),
new UIPropertyMetadata(null));
/// <summary>The popup property.</summary>
public static readonly DependencyProperty HyperlinkTextProperty = DependencyProperty.Register(
"HyperlinkText",
typeof(string),
typeof(HyperlinkPopup),
new PropertyMetadata(default(string)));
#endregion
#region Constructors and Destructors
/// <summary>Initializes static members of the <see cref="HyperlinkPopup"/> class.</summary>
static HyperlinkPopup()
{
DefaultStyleKeyProperty.OverrideMetadata(
typeof(HyperlinkPopup),
new FrameworkPropertyMetadata(typeof(HyperlinkPopup)));
}
#endregion
#region Public Properties
/// <summary>Gets or sets the popup.</summary>
public object Popup
{
get
{
return (object)this.GetValue(PopupProperty);
}
set
{
this.SetValue(PopupProperty, value);
}
}
/// <summary>Gets or sets the text.</summary>
public string HyperlinkText
{
get
{
return (string)this.GetValue(HyperlinkTextProperty);
}
set
{
this.SetValue(HyperlinkTextProperty, value);
}
}
#endregion
}
OnClick事件處理下面的方式(有點哈克,會很樂意學習如何更好地做到這一點):
/// <summary>The hyperlink popup events.</summary>
public partial class HyperlinkPopupEvents
{
#region Methods
/// <summary>The hyperlink on click.</summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The e.</param>
private void HyperlinkOnClick(object sender, RoutedEventArgs e)
{
// Locate ContentPresenter
var hyperLink = (Hyperlink)sender;
var grid = VisualTreeHelper.GetParent(hyperLink.Parent) as UIElement;
Debug.Assert(grid != null, "element is null");
Debug.Assert(
VisualTreeHelper.GetChildrenCount(grid) == 2,
"There must be only 2 children under grid in our custom HyperlinkPopup");
var popup = (Popup)VisualTreeHelper.GetChild(grid, 1);
popup.IsOpen = true;
}
#endregion
}
Generic.xaml:
<Style TargetType="{x:Type local:HyperlinkPopup}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:HyperlinkPopup}">
<Grid>
<TextBlock VerticalAlignment="Center">
<Hyperlink Click="HyperlinkOnClick">
<Run Text="{TemplateBinding HyperlinkText}"/>
</Hyperlink>
</TextBlock>
<Popup StaysOpen="False" Placement="Mouse" AllowsTransparency="True" PopupAnimation="Slide">
<ContentControl Style="{StaticResource PopupContentStyle}">
<ContentPresenter ContentSource="Popup"/>
</ContentControl>
</Popup>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
謝謝King King的解決方案!我做此練習的主要原因是刪除代碼並改進xaml代碼的表達。所以,我真的想在附近保持超鏈接和彈出式功能。 – ZakiMa 2014-09-28 22:02:24