Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a particular word(s) as hyperlink at runtime in xaml

I am using MVVM model in my silverlight(windows phone)application.I have a textblock in my view called "Status".This textblock will display the status of the transaction.If user is not registered then it shows the message "You are not registered with us.Please register here".The word "here" should be a hyper link.If user is registered with us then we say "Last logged in at xx-xx-xxxx"...

My View.xaml has:

<TextBlock Name="lblStatusValue" 
           Text="{Binding Status}"  
           TextWrapping="Wrap"   FontSize="23"  
/>                                

ViewModel.cs has a property defined for the binding of the baove control.

private string _Status;

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

Is that possible to select a particular word and make it as hyper link in any message we want to display ?Since I am using MVVM model I don't how to add the objects at runtime ( I tried this with Run control in hyperlink but in MVVM how do we achieve this ?)

Is it I have to add code like below in View.cs, and not possible to do it from the ViewModel.cs ?

wpf: How to add a Hyperlink at runtime?

like image 364
krrishna Avatar asked Oct 21 '22 21:10

krrishna


1 Answers

Instead of using a hack, why don't you try the simple and neat way? How about having two different texts? E.g.

<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>

According to whether the user is new, either the welcome text or the text link combination is shown. This SO post shows how a Hyperlink can be used.

Since I don't know whether the built-in BooleanToVisibilityConverter (doc) supports negation, I've provided you my implementation. Please note, that I've not instantiated the converter in my sample code.

[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);
    }
}

I'm assuming that the rest is clear, since you're familiar with MVVM a.s.o.

Hope this helps a bit.

like image 105
DHN Avatar answered Nov 01 '22 11:11

DHN