1

次の問題があります: WPF アプリケーションに TextBox があります。非常に長いテキスト (テキスト ボックス フィールドに表示できるよりも多くの文字) を入力し、そのテキスト ボックス フィールドから離れて (たとえば、他のテキスト ボックスに) 移動すると、入力したばかりのテキストが正しく表示されます。正当化されました(私が残した場所)。つまり、ホームキーを押すか、画面を閉じて再度開かない限り、テキストの先頭を再び見ることはできません。ウィンドウ上の別のテキスト ボックスに移動した後、テキストを左揃えにすることはできますか? おそらく「魚」のソリューションを試してみましたが、うまくいきません:

    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}")));
        }
    }
4

2 に答える 2

2

これを試して:

 textBox.SelectionStart = 0;
于 2011-09-12T10:40:15.777 に答える
1

Tim Dams の回答に関する Meleak のメモによると、添付されたビヘイビアとしてそれを行う方法は次のとおりです。

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;
    }
}

PresentationCorePresentationFrameworkSystem.XamlおよびWindowsBaseアセンブリへの参照が必要です。

使用例は次のとおりです。

<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:tbb2 番目の属性とその使用法に注意してくださいTextBox

于 2011-09-12T11:41:03.927 に答える