如何从左侧开始文本

时间:2013-03-20 11:22:22

标签: c# .net wpf xaml mvvm

我有一个可编辑的ComboBox,当添加的文本太长时,它看起来像这样:

enter image description here

如何让文本框从字符串的开头开始?

TextBox txt = sender as TextBox;
txt.Text = "[Children]";

    <Style TargetType="TextBox">
        <Setter Property="VerticalAlignment" Value="Center"/>
        <Setter Property="BorderBrush" Value="Silver"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="SnapsToDevicePixels" Value="True"/>
        <Setter Property="OverridesDefaultStyle" Value="True"/>
        <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
        <Setter Property="AllowDrop" Value="true"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TextBoxBase}">
                    <Border Name="Border" Padding="1" Background="#FFFFFF" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" >
                        <ScrollViewer Margin="0" x:Name="PART_ContentHost"/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter TargetName="Border" Property="Background" Value="#EEEEEE"/>
                            <Setter TargetName="Border" Property="BorderBrush" Value="#EEEEEE"/>
                            <Setter Property="Foreground" Value="#888888"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

组合框:

ComboBox cmbValue1 = new ComboBox();
cmbValue1.IsTextSearchEnabled = false;
cmbValue1.IsEditable = true;
cmbValue1.Width = 70;
TextBox txtEdit = (TextBox)((sender as ComboBox).Template.FindName("PART_EditableTextBox", (sender as ComboBox)));
txtEdit.Tag = selection;

1 个答案:

答案 0 :(得分:0)

您需要将选择开始设置为0,以便在用户输入内容后将其“左对齐”。

通过TextBoxBase.PreviewLostKeyboardFocus事件执行此操作 - 这是一些XAML:

 <ComboBox Name="ComboBox1"
           IsEditable="True" 
           TextBoxBase.PreviewLostKeyboardFocus="TextBox_PreviewLostKeyboardFocus_1">

事件本身:

private void TextBox_PreviewLostKeyboardFocus_1(object sender, KeyboardFocusChangedEventArgs e)
{
    TextBox txtEdit = (TextBox)((sender as ComboBox).Template.FindName("PART_EditableTextBox", (sender as ComboBox)));
    txtEdit.SelectionStart = 0;
}

我认为这应该足以让您适应您的应用程序。我在一个空的WPF应用程序中进行了测试,无论您是使用TAB还是使用鼠标单击UI的另一部分,它都能正常工作。

修改:

以下是如何在代码中添加事件:

不确定您的应用的结构,但同样,这对我有用:

private void Window_Initialized_1(object sender, EventArgs e)
{
    ComboBox cmbValue1 = new ComboBox();
    cmbValue1.IsTextSearchEnabled = false;
    cmbValue1.IsEditable = true;
    cmbValue1.Width = 70;
    cmbValue1.PreviewLostKeyboardFocus += TextBox_PreviewLostKeyboardFocus_1;

    this.MyCanvas.Children.Add(cmbValue1);
}

(使用我在上面的答案中发布的相同事件处理程序)

修改

以下是工作项目的链接:注意 - 如果在失去焦点时未选择可编辑ComboBox中的文本(即在键入内容或仅取消选择后),则它可以正常工作。我会尝试解决这个问题。

http://23.23.250.9/wpfresource.zip