Je veux avoir une ressource à chaîne, qui contient un lien hypertexte. Je suppose que ce n'est pas possible, sauf si j'avais 4 chaînes de ressources:
texte pré-hypertexte hyperlien href Texte de liens hypertexte Texte post-hypertexte. P>
puis construisez-le dans le XAML via: p> qui est juste affreux, pour de nombreuses raisons (style, pas très dynamique etc). Bar crée mon propre format de rendu et de chaîne, tels que "s'il vous plaît envoyer un courriel {me@there.com|Le Helpdesk} pour une assistance supplémentaire". Y a-t-il une autre façon d'y parvenir? (Ne doit pas nécessiter d'utiliser le fichier ressources.RESX) p> p>
3 Réponses :
En fin de compte, je viens de faire mon propre contrôle de texte de texte pour celui-ci (nommé imaginitive 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;
}
Voici ma solution:
<TextBlock x:Name="MyTextBlock" Grid.Column="1" Text="{x:Static resource:ResourceFile.Message}" Style="{StaticResource MyTextStyle}" >
<Hyperlink>
click here
</Hyperlink>
</TextBlock>
Ma solution de contournement est de déclarer "X" et "RESX" en haut du WPF XAML afin qu'il puisse voir la ressource. (Remplacez le nom de produit avec votre espace de noms pour le produit):
<TextBlock HorizontalAlignment="Center" FontSize="14">
<Run Text="{x:Static resx:Strings.Contact}"/>
<Hyperlink Command="{Binding SupportCommand}" FontStyle="Italic">
<Run Text="{x:Static resx:Strings.EmailTo}"/>
</Hyperlink>
</TextBlock>