开发者

TextBox within DataTemplate, on GotFocus cannot assign SelectionStart?

I have a logic with my textbox which says, on focus move the selection start to the last character, so that the editing person can just continue writing.

This worked perfectly with this:

    private void TextBox_GotFocus(object sender, EventArgs e)
    {
        var textBox = sender as TextBox;
        if (textBox == null) return;

        textBox.SelectionStart = textBox.Text.Length;
    }

and

    <Style TargetType="{x:Type TextBox}">
        <EventSetter Event="GotFocus" Handler="TextBox_GotFocus"/>
    </Style>

and

<DataGridTemplateColumn.CellEditingTemplate>
    <DataTemplate>
        <TextBox Name="SomeTextBox" Text="{Binding Path=Pressure, UpdateSourceTrigger=PropertyChanged}" Padding="2,0,0,0" />
        <DataTemplate.Triggers>
            <Trigger SourceName="SomeTextBox" Property="IsVisible" Value="True">
                <Setter TargetName="SomeTextBox" Property="FocusManager.FocusedElement" Value="{Binding ElementName=SomeTextBox}"/>
            </Trigger>
        </DataTemplate.Triggers>
    </DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>

but when I moved this to:

<DataGridTemplateColumn.CellEditingTemplate>
    <DataTemplate>
        <ContentControl Content="{Binding Path=Pressure, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" ContentTemplate="{StaticResource DataGridTextBoxEdit}" />
    </DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>

and a reusable template:

    <DataTemplate x:Key="DataGridTextBoxEdit">
        <TextBox Name="TextBox" Text="{Binding Content, RelativeSource={RelativeSource AncestorType=ContentControl}}" Padding="2,0,0,0" />
        <DataTemplate.Triggers>
            <Trigger SourceName="TextBox" Property="IsVisible" Value="True">
                <Setter TargetName="TextBox" Property="FocusManager.FocusedElement" Value="{Binding ElementName=TextBox}"/>
            </Trigger>
        </DataTemplate.Triggers>
    </DataTemplate>

it just stopped working. The GotFocus event fires off, however I simply cannot assign anything to SelectionStart, it just doesn't save it. Tried even hardcoding:

    private void TextBox_GotFocus(object sender, EventArgs e)
    {
        var textBox = sender as TextBox;
        if (textBox == null) return;

        textBox.SelectionStart = 5;
    }

but didn't work. It's also wo开发者_StackOverflowrth to note that Text is empty, only DataContext is filled at this point, however as the SelectionStart is not taking anything (saving), it's no good to me.

What am I doing wrong?

Kind regards, Vladan


At the point where the TextBox gets focus it does not have any text yet, this means the handler fires before the DataGrid assigns the value. One way to get arround this is to check for the first text change and then do the selection change, e.g.

private void TextBox_GotFocus(object sender, EventArgs e)
{
    var textBox = sender as TextBox;
    if (textBox == null) return;

    var desc = DependencyPropertyDescriptor.FromProperty(TextBox.TextProperty, typeof(TextBox));
    EventHandler handler = null;
    handler = new EventHandler((s, _) =>
        {
            desc.RemoveValueChanged(textBox, handler);
            textBox.SelectionStart = textBox.Text.Length;
        });
    desc.AddValueChanged(textBox, handler);
}

(This code may not be very clean, use at own risk)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜