mirror of
https://github.com/ZuneDev/ZuneModdingHelper.git
synced 2026-07-27 13:12:21 -07:00
Add SelectableTextBlock
This commit is contained in:
@@ -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>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user