first commit
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
<UserControl x:Name="root" x:Class="ClassicAssist.Launcher.Controls.EditTextBlock"
|
||||
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="300" d:DesignWidth="300">
|
||||
<Grid>
|
||||
<TextBlock x:Name="textBlock" Text="{Binding ElementName=root, Path=Text}" VerticalAlignment="Center"
|
||||
MouseDown="TextBlock_OnMouseDown" Background="Transparent" />
|
||||
<TextBox x:Name="textBox"
|
||||
Text="{Binding ElementName=root, Path=Text, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
|
||||
VerticalAlignment="Center" Visibility="Collapsed" KeyDown="TextBox_OnKeyDown"
|
||||
LostFocus="TextBox_LostFocus" />
|
||||
</Grid>
|
||||
</UserControl>
|
||||
@@ -0,0 +1,71 @@
|
||||
using System;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Threading;
|
||||
|
||||
namespace ClassicAssist.Launcher.Controls
|
||||
{
|
||||
/// <summary>
|
||||
/// Interaction logic for EditTextBlock.xaml
|
||||
/// </summary>
|
||||
public partial class EditTextBlock
|
||||
{
|
||||
public static readonly DependencyProperty TextProperty = DependencyProperty.Register( "Text", typeof( string ),
|
||||
typeof( EditTextBlock ), new UIPropertyMetadata() );
|
||||
|
||||
public static readonly DependencyProperty CanEditProperty = DependencyProperty.Register( "CanEdit",
|
||||
typeof( bool ), typeof( EditTextBlock ), new UIPropertyMetadata( true ) );
|
||||
|
||||
public EditTextBlock()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
public bool CanEdit
|
||||
{
|
||||
get => (bool) GetValue( CanEditProperty );
|
||||
set => SetValue( CanEditProperty, value );
|
||||
}
|
||||
|
||||
public string Text
|
||||
{
|
||||
get => (string) GetValue( TextProperty );
|
||||
set => SetValue( TextProperty, value );
|
||||
}
|
||||
|
||||
private void TextBlock_OnMouseDown( object sender, MouseButtonEventArgs e )
|
||||
{
|
||||
if ( e.ClickCount <= 1 )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if ( !CanEdit )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
( (TextBlock) sender ).Visibility = Visibility.Collapsed;
|
||||
textBox.Visibility = Visibility.Visible;
|
||||
textBox.CaretIndex = textBox.Text.Length;
|
||||
textBox.SelectAll();
|
||||
|
||||
Dispatcher?.BeginInvoke( (Action) ( () => Keyboard.Focus( textBox ) ), DispatcherPriority.Render );
|
||||
}
|
||||
|
||||
private void TextBox_OnKeyDown( object sender, KeyEventArgs e )
|
||||
{
|
||||
if ( e.Key == Key.Return )
|
||||
{
|
||||
TextBox_LostFocus( sender, null );
|
||||
}
|
||||
}
|
||||
|
||||
private void TextBox_LostFocus( object sender, RoutedEventArgs e )
|
||||
{
|
||||
textBlock.Visibility = Visibility.Visible;
|
||||
( (TextBox) sender ).Visibility = Visibility.Collapsed;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user