Reputation: 1001
I am building a string in which I have to hide an email address.
string StatusText = "Please reach out here for access."
Just wanted to know how can we hide an email address in a string. I'd like the "here" to be an email address. I have to bind StatusText to a Textblock such that when user clicks on "here", the outlook mail should open.
Here's the detailed code:
View.xaml
<TextBlock Text="{Binding StatusText}" />
ViewModel.cs
private string _statusText;
public string StatusText
{
get { return _statusText; }
set { SetProperty(ref _statusText, value); }
}
DisplayMessages()
{
//based on the boolean value, Status Text is set
//When the case is "NoAccess", I want the "here" of StatusText to be clickable and show the mailaddress as [email protected]
switch(Flag)
case IsReady:
StatusText = "Application is Ready";
break;
case NoAccess:
StatusText = "Please reach out here. No access can be provided."
break;
}
Upvotes: 2
Views: 388
Reputation: 76
Step 1: Add the following XAML code inside Window.Resources.
<local:DataTemplateSelectorExt x:Key="Selector"></local:DataTemplateSelectorExt>
<DataTemplate x:Key="NoAccess">
<TextBlock Background="Green">
<Run Text="Please reach out "/>
<Hyperlink NavigateUri="[email protected]" RequestNavigate="Hyperlink_RequestNavigate">here</Hyperlink>
<Run Text=" for access."/>
</TextBlock>
</DataTemplate>
Step 2: Replace TextBlock XAML code with the following code snippet.
<Label Content="{Binding StatusText}" ContentTemplateSelector="{Binding Source={StaticResource Selector}}" />
Step 3: Add the following method inside MainWindow class.
private void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e)
{
Popup popup = new Popup { IsOpen = true, Width = 100, Height = 50, Placement = PlacementMode.Mouse };
popup.Child = new TextBlock { Text = (sender as Hyperlink).NavigateUri.ToString(), Background = Brushes.Green };
popup.IsOpen = true;
}
Step 4: Finally add the below class in your project.
public class DataTemplateSelectorExt : DataTemplateSelector
{
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
if (item == null) return (DataTemplate)null;
FrameworkElement frameworkElement = container as FrameworkElement;
if (item.Equals("Please reach out here. No access can be provided."))
return Application.Current.MainWindow.FindResource("NoAccess") as DataTemplate;
return null;
}
}
Hope, this will meet your requirement.
Upvotes: 2
Reputation: 4046
I would you to take advantage of XAML
.
<TextBlock>
<Run Text="Please reach out "/>
<Hyperlink NavigateUri="YOUR_URL_FOR_OUTLOOK" RequestNavigate="Hyperlink_RequestNavigate">here</Hyperlink>
<Run Text=" for access."/>
</TextBlock>
In code behind
private void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e)
{
Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri));
e.Handled = true;
}
The main benefit of using this approach is that you can use Binding
to bind your emailId to Hyperlink
.
Your output will look like this in Viewer
Upvotes: 2