Add SelectableTextBlock

This commit is contained in:
Yoshi Askharoun
2024-05-16 21:09:15 -05:00
parent f234433cf2
commit ef97d4ef13
2 changed files with 49 additions and 2 deletions
@@ -0,0 +1,47 @@
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
namespace ZuneModdingHelper.Controls;
// https://stackoverflow.com/a/32870521
public class SelectableTextBlock : TextBlock
{
private TextPointer _startSelectPosition;
private TextPointer _endSelectPosition;
public string SelectedText { get; protected set; } = "";
public delegate void TextSelectedHandler(string SelectedText);
public event TextSelectedHandler TextSelected;
protected override void OnMouseDown(MouseButtonEventArgs e)
{
base.OnMouseDown(e);
Point mouseDownPoint = e.GetPosition(this);
_startSelectPosition = GetPositionFromPoint(mouseDownPoint, true);
// TODO: Is there any better way to allow text to be selected?
Clipboard.SetText(Text);
}
protected override void OnMouseUp(MouseButtonEventArgs e)
{
base.OnMouseUp(e);
Point mouseUpPoint = e.GetPosition(this);
_endSelectPosition = GetPositionFromPoint(mouseUpPoint, true);
TextRange otr = new(ContentStart, ContentEnd);
otr.ApplyPropertyValue(TextElement.ForegroundProperty, Foreground);
otr.ApplyPropertyValue(TextElement.BackgroundProperty, Background);
TextRange ntr = new(_startSelectPosition, _endSelectPosition);
ntr.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(Colors.Black));
ntr.ApplyPropertyValue(TextElement.BackgroundProperty, new SolidColorBrush(Colors.HotPink));
SelectedText = ntr.Text;
TextSelected?.Invoke(SelectedText);
}
}
@@ -45,8 +45,8 @@
Foreground="{StaticResource ZuneMediumTextBrush}"/>
<StackPanel Grid.Row="1" Grid.ColumnSpan="2">
<TextBlock Text="{Binding Description}" FontSize="14" TextWrapping="Wrap" Margin="0,0,0,12"
Foreground="{StaticResource ZuneLightTextBrush}"/>
<local:SelectableTextBlock Text="{Binding Description}" FontSize="14" TextWrapping="Wrap" Margin="0,0,0,12"
Foreground="{StaticResource ZuneLightTextBrush}"/>
<ContentControl x:Name="InnerPresenter" />
</StackPanel>