问题描述:

I have a text box that is only 10 characters wide. I need to put 20 characters in it, but only need to show the last 10 characters. I need to force the TextBox to always overflow the text to the left, thus allowing the 10 characters on the right to be visible.

I have looked at several postings and tried all the recommendations I could find, but they don't seem to work properly.

Here are the things I have tried so far (by themselves and in combinations):

<TextBox

HorizontalContentAlignment="Right"

TextAlignment="Right"

FlowDirection="RightToLeft"/>

Seems like FlowDirection is the closest thing to get it to work, but it makes the left and right arrow keys operate backwords.. (you press the left key, the cursor inside the TextBox moves to the right) Also, when you tab off the control after editing the left most text, the text doesn't pop back and show the rightmost characters again.

TextAlignment will just push the text to the right if there isn't enough text to fill the box.

HorizontalContentAlignment doesn't appear to be helping at all...

网友答案:

I made this small example:

First, the XAML of the window:

<Window x:Class="WpfApplication3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel>
        <TextBox Text="1234567890" Width="40" Name="MyTextBox" LostFocus="MyTextBox_LostFocus" />
        <TextBox Text="" Width="40" Name="MyTextBox2" />
    </StackPanel>
</Window>

And some code behind:

    private void MyTextBox_LostFocus(object sender, RoutedEventArgs e) {
        MyTextBox.ScrollToHorizontalOffset(999999);
    }

This will only accomplish what you're looking for when the focus is lost. You may have to call ScrollToHorizontalOffset() after your view is loaded to have the TextBox start off that way. And there might be a better way to determine the parameter. double.MaxValue might be a better choice.

相关阅读:
Top