C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Here: The appearance attributes we set are not important. They just make the block look different.
And: We add MouseEnter and MouseLeave attributes. Type "MouseEnter" and then have Visual Studio add the event handler.
Example markup: XAML
<Window x:Class="WpfApplication3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TextBlock HorizontalAlignment="Left"
Margin="10,10,0,0"
TextWrapping="Wrap"
Text="Hello"
VerticalAlignment="Top"
FontFamily="Georgia"
Background="AliceBlue"
Padding="40"
Width="200"
MouseEnter="TextBlock_MouseEnter"
MouseLeave="TextBlock_MouseLeave"/>
</Grid>
</Window>
Example code: C#
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace WpfApplication3
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void TextBlock_MouseEnter(object sender, MouseEventArgs e)
{
// ... Change Text to Enter when mouse enters.
var block = sender as TextBlock;
if (block != null)
{
block.Text = "Enter";
}
}
private void TextBlock_MouseLeave(object sender, MouseEventArgs e)
{
// ... Change Text to Leave when mouse leaves.
var block = sender as TextBlock;
if (block != null)
{
block.Text = "Leave";
}
}
}
}
FontFamily: This indicates the font, such as "Georgia" or "Verdana". A string value here is sufficient.
Background: This is the background color of the block. AliceBlue is a light blue shade. The background is the "back color" of the control.
ForeColor, BackColorPadding: The padding of the TextBlock in pixels. The 40-pixel padding expands the colored block of the control.
Width: This indicates the number of pixels the TextBlock is wide. If the block is too small, the text is not fully shown.
Note: We can get the originating object for the MouseEnter event by casting the sender object to a TextBlock.
ObjectAsAlso: The MouseLeave event can be used in the same way as the MouseEnter event. This event handler can "erase" the changes made by Enter.
EventNoWrap: With this TextWrapping option, the entire string contents will render in one line. No breaks are rendered.
Wrap: All lines will wrap. If a break point is not found, one will be added in the middle of a word.
WrapWithOverflow: Same as Wrap except a long word will not be broken into more than one line—it will just overflow.
Example markup 2: XAML
<Window x:Class="WpfApplication8.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TextBlock HorizontalAlignment="Left"
Margin="10"
TextWrapping="Wrap"
Text="Initial"
VerticalAlignment="Top"
Loaded="TextBlock_Loaded"/>
</Grid>
</Window>
Example code 2: C#
using System.Windows;
using System.Windows.Controls;
namespace WpfApplication8
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void TextBlock_Loaded(object sender, RoutedEventArgs e)
{
// Get TextBlock reference.
var block = sender as TextBlock;
// Set text.
block.Text = "This is some longer text in the TextBlock. " +
"We see how it wraps in this example. " +
"TextBlock is meant for longer text.";
}
}
}