5

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?

Community
  • 1
  • 1
krrishna
  • 2,050
  • 4
  • 47
  • 101
  • This [link](http://stackoverflow.com/questions/3770197/wpf-how-to-add-a-hyperlink-at-runtime) deals similar thread. that may be helpful for you. – SaravanaKumar Oct 31 '13 at 13:38
  • I think to avoid the most disruption to your implementation (sounds like in the same `TextBlock` you want to have mixed text _and_ a link), you could consider using a `RichTextBox`. With it you can define hyperlinks inline: http://msdn.microsoft.com/en-us/library/ee681613%28v=vs.95%29.aspx#hyperlinks – Chris Sinclair Oct 31 '13 at 13:39
  • a `TemplateSelector` seems appropriate here. However you can't use it with a `TextBlock` so I would consider swiching to a `ContentControl` with a `TemplateSelector` with two `DataTemplate`s, one with hyperlink and one without it ... – Omri Btian Oct 31 '13 at 13:43
  • look up how to use Paragraph in xaml . than you can separate your text into parts. – eran otzap Oct 31 '13 at 13:45
  • maybe this may help: http://stackoverflow.com/questions/2092890/add-hyperlink-to-textblock-wpf – lena Nov 01 '13 at 06:24

1 Answers1

3

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.

Community
  • 1
  • 1
DHN
  • 4,807
  • 3
  • 31
  • 45