如何在XAML中使特定的单词在运行时成为超链接

5
我在我的Silverlight(Windows Phone)应用程序中使用MVVM模型。我的视图中有一个名为“状态”的TextBlock。这个TextBlock将显示交易的状态。如果用户没有注册,则显示消息“您尚未在我们这里注册。请在此处注册”。单词“此处”应该是超链接。如果用户已经在我们这里注册,那么我们会说“上次登录时间为xx-xx-xxxx”...
我的View.xaml包含:
<TextBlock Name="lblStatusValue" 
           Text="{Binding Status}"  
           TextWrapping="Wrap"   FontSize="23"  
/>                                

ViewModel.cs 中定义了一个属性,用于绑定上面的控件。

private string _Status;

public string Status
{
    get { return _Status; }
    set
    {
        if (value != _Status)
        {
            _Status = value;
            RaisePropertyChanged("Status");
        }
    }
}

我们是否可以在任何要显示的消息中选择特定单词并将其作为超链接?由于我正在使用MVVM模型,我不知道如何在运行时添加对象(我尝试了使用超链接中的Run控件,但在MVVM中我们如何实现这一点?)

我是否必须像下面的代码一样在View.cs中添加代码,而无法从ViewModel.cs中执行此操作?

wpf:如何在运行时添加超链接?


这个链接涉及到类似的主题,可能对你有帮助。 - SaravanaKumar
我认为为了避免对你的实现造成最大的干扰(听起来好像在同一个“TextBlock”中,你想要混合文本和链接),你可以考虑使用“RichTextBox”。通过它,你可以内联定义超链接:http://msdn.microsoft.com/en-us/library/ee681613%28v=vs.95%29.aspx#hyperlinks - Chris Sinclair
这里似乎适合使用TemplateSelector模板选择器。但是,你不能在TextBlock上使用它,因此我建议切换到一个带有两个DataTemplateContentControl,一个带有超链接,另一个没有... - Omri Btian
查找如何在XAML中使用段落,然后您可以将文本分成部分。 - eran otzap
也许这个可以帮到你:https://dev59.com/eXI95IYBdhLWcg3w7CjL - lena
1个回答

3

不要用 hack,为什么不试试简单而整洁的方法呢?两种不同的文本怎么样?例如:

<Grid>
    <Label Text="{Binding YourNormalTextComesHere}" 
           Visibility="{Binding IsUserNew, Converter={StaticResource BoolToVisibilityConv}, ConverterParameter=Not}" />
    <StackPanel Orientation=Horizontal 
                Visibilty="{Binding IsUserNew, Converter={StaticResource BoolToVisibilityConv}}">
        <Label Text="Your not registered with us. Please register "/>
        <HyperLink NavigateUri="...">here</HyperLink>
    </StackPanel>
</Grid>

根据用户是否为新用户,显示欢迎文本或文本链接组合。这篇SO帖子展示了如何使用Hyperlink。由于我不知道内置的BooleanToVisibilityConverter文档)是否支持否定,所以提供了我的实现。请注意,我在示例代码中没有实例化转换器。
[ValueConversion(typeof (bool?), typeof (Visibility))]
public class BoolToVisibilityConverter : IValueConverter
{
    public const string Invert = "Not";
    private const string TypeIsNotAllowed = "The type '{0}' is not allowed.";

    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var boolValue = value as bool?;
        if (boolValue == null)
            throw new NotSupportedException(String.Format(TypeIsNotAllowed, value.GetType().Name));

        return ((boolValue.Value && !IsInverted(parameter)) || (!boolValue.Value && IsInverted(parameter))) 
            ? Visibility.Visible 
            : Visibility.Collapsed;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var visibilityValue = value as Visibility?;
        if (visibilityValue == null)
            throw new NotSupportedException(String.Format(TypeIsNotAllowed, value.GetType().Name));

        return visibilityValue == Visibility.Visible && !IsInverted(parameter);
    }

    #endregion

    private static bool IsInverted(object param)
    {
        var strParam = param as string;
        if (param == null || string.IsNullOrEmpty(strParam))
            return false;

        return strParam.Equals(Invert, StringComparison.InvariantCultureIgnoreCase);
    }
}

我假设你对MVVM等有所熟悉,因此剩下的内容应该比较清晰明了。

希望这能有所帮助。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接