如何在xaml中在运行时将特定单词作为超链接

krr*_*hna 5 c# silverlight wpf xaml mvvm

我在我的silverlight(windows phone)应用程序中使用MVVM模型.我的视图中有一个名为"Status"的文本块.此文本块将显示事务的状态.如果用户未注册,则会显示消息"你不是在这里注册.请在这里注册"."这里"这个词应该是超链接.如果用户在我们这里注册,那么我们说"最后登录xx-xx-xxxx"...

我的View.xaml有:

<TextBlock Name="lblStatusValue" 
           Text="{Binding Status}"  
           TextWrapping="Wrap"   FontSize="23"  
/>                                
Run Code Online (Sandbox Code Playgroud)

ViewModel.cs具有为baove控件的绑定定义的属性.

private string _Status;

public string Status
{
    get { return _Status; }
    set
    {
        if (value != _Status)
        {
            _Status = value;
            RaisePropertyChanged("Status");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

我是否必须在View.cs中添加如下代码,而不能从ViewModel.cs中执行此操作?

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

DHN*_*DHN 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>
Run Code Online (Sandbox Code Playgroud)

根据用户是否是新用户,显示欢迎文本或文本链接组合。这篇SO 文章展示了如何Hyperlink使用 a 。

由于我不知道内置BooleanToVisibilityConverterdoc)是否支持否定,因此我向您提供了我的实现。请注意,我没有在示例代码中实例化转换器。

[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);
    }
}
Run Code Online (Sandbox Code Playgroud)

我假设剩下的很清楚,因为你熟悉MVVMaso

希望这个对你有帮助。