<UserControl x:Class="XFEToolBox.WpfCore.Controls.ScrollTextBlock"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="24" d:DesignWidth="160"
x:Name="scrollTextBlock"
Loaded="ScrollTextBlock_Loaded"
Unloaded="ScrollTextBlock_Unloaded"
SizeChanged="ScrollTextBlock_SizeChanged"
MouseEnter="ScrollTextBlock_MouseEnter"
MouseLeave="ScrollTextBlock_MouseLeave">
<Grid ClipToBounds="True">
<!-- 非悬停状态始终保留省略号,避免长文本直接被硬裁切。 -->
<TextBlock x:Name="ellipsisTextBlock"
Text="{Binding InnerText, ElementName=scrollTextBlock}"
Foreground="{Binding InnerForeground, ElementName=scrollTextBlock}"
Background="{Binding InnerBackground, ElementName=scrollTextBlock}"
FontSize="{Binding InnerFontSize, ElementName=scrollTextBlock}"
FontFamily="{Binding InnerFontFamily, ElementName=scrollTextBlock}"
FontWeight="{Binding InnerFontWeight, ElementName=scrollTextBlock}"
TextAlignment="{Binding InnerTextAlignment, ElementName=scrollTextBlock}"
TextDecorations="{Binding InnerTextDecorations, ElementName=scrollTextBlock}"
VerticalAlignment="{Binding InnerTextVerticalAlignment, ElementName=scrollTextBlock}"
HorizontalAlignment="Stretch"
TextWrapping="NoWrap"
TextTrimming="CharacterEllipsis"/>
<!-- 只有文本确实溢出且鼠标悬停时才显示滚动层。Hidden 保证可测得完整文本宽度。 -->
<ScrollViewer x:Name="scrollViewer"
Visibility="Hidden"
HorizontalScrollBarVisibility="Hidden"
VerticalScrollBarVisibility="Disabled"
Focusable="False">
<StackPanel x:Name="stackPanel" Orientation="Horizontal"
VerticalAlignment="Center" HorizontalAlignment="Left">
<TextBlock x:Name="textBlock"
Text="{Binding InnerText, ElementName=scrollTextBlock}"
Foreground="{Binding InnerForeground, ElementName=scrollTextBlock}"
Background="{Binding InnerBackground, ElementName=scrollTextBlock}"
FontSize="{Binding InnerFontSize, ElementName=scrollTextBlock}"
FontFamily="{Binding InnerFontFamily, ElementName=scrollTextBlock}"
FontWeight="{Binding InnerFontWeight, ElementName=scrollTextBlock}"
TextAlignment="Left"
TextDecorations="{Binding InnerTextDecorations, ElementName=scrollTextBlock}"
VerticalAlignment="{Binding InnerTextVerticalAlignment, ElementName=scrollTextBlock}"
TextWrapping="NoWrap"/>
</StackPanel>
</ScrollViewer>
</Grid>
</UserControl>
<UserControl x:Class="XFEToolBox.WpfCore.Controls.ScrollTextBlock"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="24" d:DesignWidth="160"
x:Name="scrollTextBlock"
Loaded="ScrollTextBlock_Loaded"
Unloaded="ScrollTextBlock_Unloaded"
SizeChanged="ScrollTextBlock_SizeChanged"
MouseEnter="ScrollTextBlock_MouseEnter"
MouseLeave="ScrollTextBlock_MouseLeave">
<Grid ClipToBounds="True">
<!-- 非悬停状态始终保留省略号,避免长文本直接被硬裁切。 -->
<TextBlock x:Name="ellipsisTextBlock"
Text="{Binding InnerText, ElementName=scrollTextBlock}"
Foreground="{Binding InnerForeground, ElementName=scrollTextBlock}"
Background="{Binding InnerBackground, ElementName=scrollTextBlock}"
FontSize="{Binding InnerFontSize, ElementName=scrollTextBlock}"
FontFamily="{Binding InnerFontFamily, ElementName=scrollTextBlock}"
FontWeight="{Binding InnerFontWeight, ElementName=scrollTextBlock}"
TextAlignment="{Binding InnerTextAlignment, ElementName=scrollTextBlock}"
TextDecorations="{Binding InnerTextDecorations, ElementName=scrollTextBlock}"
VerticalAlignment="{Binding InnerTextVerticalAlignment, ElementName=scrollTextBlock}"
HorizontalAlignment="Stretch"
TextWrapping="NoWrap"
TextTrimming="CharacterEllipsis"/>
<!-- 只有文本确实溢出且鼠标悬停时才显示滚动层。Hidden 保证可测得完整文本宽度。 -->
<ScrollViewer x:Name="scrollViewer"
Visibility="Hidden"
HorizontalScrollBarVisibility="Hidden"
VerticalScrollBarVisibility="Disabled"
Focusable="False">
<StackPanel x:Name="stackPanel" Orientation="Horizontal"
VerticalAlignment="Center" HorizontalAlignment="Left">
<TextBlock x:Name="textBlock"
Text="{Binding InnerText, ElementName=scrollTextBlock}"
Foreground="{Binding InnerForeground, ElementName=scrollTextBlock}"
Background="{Binding InnerBackground, ElementName=scrollTextBlock}"
FontSize="{Binding InnerFontSize, ElementName=scrollTextBlock}"
FontFamily="{Binding InnerFontFamily, ElementName=scrollTextBlock}"
FontWeight="{Binding InnerFontWeight, ElementName=scrollTextBlock}"
TextAlignment="Left"
TextDecorations="{Binding InnerTextDecorations, ElementName=scrollTextBlock}"
VerticalAlignment="{Binding InnerTextVerticalAlignment, ElementName=scrollTextBlock}"
TextWrapping="NoWrap"/>
</StackPanel>
</ScrollViewer>
</Grid>
</UserControl>