Hi, i am using custom controls to make stuff behave like i want it to.
In this control i use a double click to swap the label to a textbox, so the user can edit the text, and then press enter OR click somewhere else to lose focus and switch back to the label with the new text. But the textbox does not lose focus and im not sure how to fix this
XML:
<UserControl x:Class="DisSFCEdit.Comment" 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" xmlns:local="clr-namespace:DisSFCEdit" mc:Ignorable="d" d:DesignHeight="40" d:DesignWidth="100" MaxHeight="40px" MinWidth="90px" Height="40px" LostFocus="UserControl_LostFocus"><Grid FocusManager.IsFocusScope="True"><Border BorderBrush="White" BorderThickness="0" Margin="1" CornerRadius="2" Background="White"><Grid Background="White" Margin="2"><Rectangle Fill="White" Margin="2" /><Label Content="{Binding Path=ContentTextComment}" Foreground="Black" Name="lblText" MouseDoubleClick="lblText_MouseDoubleClick" HorizontalAlignment="Center" VerticalAlignment="Center"/><TextBox Name="txtText" Text="{Binding Path=ContentTextComment}" Visibility="Hidden" LostFocus="txtText_LostFocus" Background="Yellow" KeyDown="txtText_KeyDown" Margin="2" FontWeight="Bold" /></Grid></Border></Grid></UserControl>C#:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace DisSFCEdit { /// <summary> /// Interaction logic for Comment.xaml /// </summary> public partial class Comment : UserControl { private string lblOld; public ObjectType Type { get { return ObjectType.Comment; } } private int _row; private int _column; public int Row { get { return _row; } set { _row = value; } } public int Column { get { return _column; } set { _column = value; } } public Comment() { InitializeComponent(); (this.Content as FrameworkElement).DataContext = this; } public static readonly DependencyProperty ContentTextProperty = DependencyProperty.Register( "ContentTextComment", typeof(string), typeof(Comment)); private static void Changed(DependencyObject d, DependencyPropertyChangedEventArgs e) { var c = d as Comment; // now, do something } public string ContentTextComment { get { return (string)GetValue(ContentTextProperty); } set { SetValue(ContentTextProperty, value); } }// ContentTextComment private void lblText_MouseDoubleClick(object sender, MouseButtonEventArgs e) { using (var d = Dispatcher.DisableProcessing()) { lblText.Visibility = Visibility.Hidden; txtText.Visibility = Visibility.Visible; lblOld = ContentTextComment; txtText.Focus(); txtText.SelectAll(); } System.Threading.ThreadPool.QueueUserWorkItem( (a) => { System.Threading.Thread.Sleep(100); txtText.Dispatcher.Invoke( new Action(() => { txtText.Focus(); })); } ); } private void txtText_LostFocus(object sender, RoutedEventArgs e) { lblText.Visibility = Visibility.Visible; txtText.Visibility = Visibility.Hidden; } private void txtText_KeyDown(object sender, KeyEventArgs e) { if ((e.Key == Key.Enter) || (e.Key == Key.Escape)) { ContentTextComment = txtText.Text; lblText.Visibility = Visibility.Visible; txtText.Visibility = Visibility.Hidden; e.Handled = true; if (e.Key == Key.Escape) { ContentTextComment = lblOld; } } } private void UserControl_LostFocus(object sender, RoutedEventArgs e) { lblText.Visibility = Visibility.Visible; txtText.Visibility = Visibility.Hidden; } } }