2011-09-12 1 views
1

다음과 같은 문제가 있습니다. WPF 응용 프로그램에 TextBox가 있습니다. 매우 긴 텍스트 (텍스트 필드에 표시 할 수있는 것보다 많은 문자) 을 입력하고 해당 텍스트 필드 (예 : 다른 텍스트 상자)로 이동하는 것보다 방금 입력 한 텍스트가 올바르게 유지됩니다. - 정당한 (내가 그것을 놓고 갔던 곳). 즉, 홈 키를 누르거나 화면을 닫은 다음 다시 열 때까지 텍스트의 시작 부분을 다시 볼 수 없습니다. 창에서 다른 텍스트 상자로 이동 한 후 텍스트를 왼쪽으로 정렬 할 수 있습니까? 나는 대부분의 아마 "물고기"솔루션을 시도하고 작동하지 않습니다텍스트가 텍스트 상자 너비보다 긴 경우 포커스 손실 후 TextBox 및 TextAlignment

private void TextEditControl_LostFocus(object sender, RoutedEventArgs e) 
    { 
     var textBox = sender as TextBox; 
     if (textBox != null) 
     { 
      textBox.Dispatcher.BeginInvoke(
       DispatcherPriority.Send, 
       new Action(() => SendKeys.SendWait("{HOME}"))); 
     } 
    } 

답변

2

이 시도해보십시오 팀 댐 '대답에 Meleak 주 당으로

textBox.SelectionStart = 0; 
+0

+1, 사이드 노트에서 재사용을 위해 첨부 된 동작으로 쉽게 전환 할 수 있음 –

1

을, 여기 당신이 그것을 할 방법 첨부 된 행동 :

using System.Windows; 
using System.Windows.Controls; 

public static class TextBoxBehavior 
{ 
    public static bool GetHomeOnLostFocus(DependencyObject obj) 
    { 
     return (bool)obj.GetValue(HomeOnLostFocusProperty); 
    } 

    public static void SetHomeOnLostFocus(DependencyObject obj, bool value) 
    { 
     obj.SetValue(HomeOnLostFocusProperty, value); 
    } 

    // Using a DependencyProperty as the backing store for HomeOnLostFocus. 
    // This enables animation, styling, binding, etc... 
    public static readonly DependencyProperty HomeOnLostFocusProperty = 
     DependencyProperty.RegisterAttached(
      "HomeOnLostFocus", 
      typeof(bool), 
      typeof(TextBoxBehavior), 
      new UIPropertyMetadata(false, OnHomeOnLostFocusChanged)); 

    public static void OnHomeOnLostFocusChanged(
     DependencyObject d, 
     DependencyPropertyChangedEventArgs e) 
    { 
     // Type checking and casting of parameters 
     bool oldVal = (bool)e.OldValue; 
     bool newVal = (bool)e.NewValue; 
     TextBox textBox = d as TextBox; 

     // Argument value tests 
     if (textBox == null) return; 
     if (oldVal == newVal) return; 

     // If HomeOnLostFocus then add event handler, otherwise, remove it. 
     if (newVal) 
      textBox.LostFocus += TextBox_LostFocus; 
     else 
      textBox.LostFocus -= TextBox_LostFocus; 
    } 

    static void TextBox_LostFocus(object sender, RoutedEventArgs e) 
    { 
     var textBox = (TextBox)sender; 
     textBox.SelectionStart = 0; 
    } 
} 

PresentationCore, PresentationFramework, System.XamlWindowsBase 어셈블리에 대한 참조를해야합니다.

<Window x:Class="WpfApplication1.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:tbb="clr-namespace:TextBoxBehavior;assembly=TextBoxBehavior" 
     Title="MainWindow" Height="350" Width="525"> 
    <StackPanel> 
     <TextBox Width="200"/> 
     <TextBox tbb:TextBoxBehavior.HomeOnLostFocus="true" Width="200"/> 
     <Button Content="Dummy" Width="200"/> 
    </StackPanel> 
</Window> 

참고 xmlns:tbb 속성과 2 TextBox에서의 사용 :

다음은 사용 예입니다.

+0

Uf ... 정말 고마워요 ... 작동합니다! – SebastijanP