2011-01-28 18 views
7

我想要一個字符串資源,其中包含一個超鏈接。我想這是不可能的,除非我有4個資源字符串:帶有超鏈接的WPF Resources.resx字符串?

預超鏈接文本超鏈接 HREF 超鏈接文本 後超鏈接文本。

<StackPanel Grid.Column="1" Orientation="Horizontal" VerticalAlignment="Top" HorizontalAlignment="Right"> 
    <TextBlock Grid.Column="1" Text="{x:Static p.Resources.PreHText}" /> 
    <Hyperlink Grid.Column="1" NavigateUri="{x:Static p.Resources.HHref}"> 
    <TextBlock Text="{x:Static p.Resources.HText}" /></Hyperlink></TextBlock> 
    <TextBlock Grid.Column="1" Text="{x:Static p.Resource.PostHText}" /> 
</StackPanel> 

這僅僅是可怕的,對於很多原因很多(造型,不是很動態等等等等):

然後通過構建其在XAML。 Bar創建我自己的渲染和字符串格式,例如「請發郵件{[email protected] |幫助臺}以獲得進一步幫助」。有沒有其他方法可以實現這一點? (不必使用resources.resx文件)

回答

1

在我只是做了我自己的文字塊控制(Imaginitively命名AdvancedTextBlock)結束:

public class AdvancedTextBlock : TextBlock { 
     new private String Text { get; set; } //prevent text from being set as overrides all I do here. 
     private String _FormattedText = String.Empty; 
     public String FormattedText { 
      get { return _FormattedText; } 
      set { _FormattedText = value; AssignInlines(); } 
     } 
     private static Regex TagRegex = new Regex(@"\{(?<href>[^\|]+)\|?(?<text>[^}]+)?}", RegexOptions.Compiled); 

     public AdvancedTextBlock() : base() { } 
     public AdvancedTextBlock(System.Windows.Documents.Inline inline) : base(inline) { } 

     public void AssignInlines(){ 
      this.Inlines.Clear(); 
      Collection<Hyperlink> hyperlinks = new Collection<Hyperlink>(); 
      Collection<String> replacements = new Collection<String>(); 
      MatchCollection mcHrefs = TagRegex.Matches(FormattedText); 
      foreach (Match m in mcHrefs) { 
       replacements.Add(m.Value); 
       Hyperlink hp = new Hyperlink(); 
       hp.NavigateUri = new Uri(m.Groups["href"].Value); 
       hp.Inlines.Add(m.Groups["text"].Success ? m.Groups["text"].Value : m.Groups["href"].Value); 
       hp.RequestNavigate += new RequestNavigateEventHandler(hp_RequestNavigate); 
       hyperlinks.Add(hp); 
      } 
      String[] sections = FormattedText.Split(replacements.ToArray(), StringSplitOptions.None); 
      hyperlinks.DefaultIfEmpty(null); 
      for (int i = 0, l = sections.Length; i < l; i++) { 
       this.Inlines.Add(sections.ElementAt(i)); 
       if (hyperlinks.ElementAtOrDefault(i) != null) { 
        this.Inlines.Add(hyperlinks[i]); 
       } 
      } 
     } 

     void hp_RequestNavigate(object sender, RequestNavigateEventArgs e) { 
      RequestNavigate(sender, e); 
     } 

     // 
     // Summary: 
     //  Occurs when navigation events are requested. 
     public event RequestNavigateEventHandler RequestNavigate; 
    } 

的只有兩件事我不太高興我的執行是:

A)我不得不劈隱藏現有的Text屬性,因爲我不知道如何防止財產重寫的東西,我做

B)(關於A)每次設置FormattedText字段(應該只有一次validtidly),我必須調用AssignInlines,但是這又一次,不知道如何掛接到實際執行的方法顯示的東西(期望找到一個PreRender,渲染事件或類似的,但我不能),所以如果有人知道如何,那很棒:)。

-2

這裏是我的解決方案:

<TextBlock x:Name="MyTextBlock" Grid.Column="1" Text="{x:Static resource:ResourceFile.Message}" Style="{StaticResource MyTextStyle}" > 
    <Hyperlink> 
      click here 
    </Hyperlink> 
</TextBlock>