This is a migrated thread and some comments may be shown as answers.

Mulitline textbox AcceptsReturn not working.

6 Answers 240 Views
DataForm
This is a migrated thread and some comments may be shown as answers.
jen
Top achievements
Rank 1
jen asked on 28 Jun 2013, 07:42 PM
I have a mulitline textbox in my raddataform's edit template, with AcceptsReturn="True".
When the user hits enter in this feild, instead of getting a new line, the dataform submits and ends the edit. 

This thread seems to indicate this shouldn't be an issue:
 http://www.telerik.com/community/forums/silverlight/data-form/multiline-dataformdatafield.aspx 

How can I get this to work correctly?
I am using the lastest release 2013 Q2.
Thanks.

<DataTemplate x:Key="Note_EditTemplate">

<StackPanel>

<telerik:DataFormDataField DataMemberBinding="{Binding Note, Mode=TwoWay}" Label="Note :" >

<TextBox Text="{Binding Note, Mode=TwoWay}" TextWrapping="Wrap" Height="100" Width="300" AcceptsReturn="True" MaxLength="1024"/>

</telerik:DataFormDataField>

<telerik:DataFormDataField DataMemberBinding="{Binding Created, StringFormat=d}" Label="Created :" IsReadOnly="True" />

<telerik:DataFormDataField DataMemberBinding="{Binding Modified, StringFormat=d}" Label="Modified :" IsReadOnly="True" />

</StackPanel>

</DataTemplate>

 

<telerik:RadDataForm Grid.Column="1" x:Name="data_UnitNotes" ItemsSource="{Binding UnitNotes.View}" VerticalAlignment="Top"

CommandButtonsVisibility="Add,Edit,Delete,Cancel,Commit" AutoGenerateFields="False" Margin="5,0,0,0"

AddedNewItem="data_UnitNotes_AddedNewItem_1" DeletedItem="data_UnitNotes_DeletedItem_1"

BeginningEdit="data_UnitNotes_BeginningEdit_1" EditEnded="data_UnitNotes_EditEnded_1"

EditTemplate="{StaticResource Note_EditTemplate}"

ReadOnlyTemplate="{StaticResource Note_ReadTemplate}"

 NewItemTemplate="{StaticResource Note_EditTemplate}"/>

 

 

 

6 Answers, 1 is accepted

Sort by
0
Ivan Ivanov
Telerik team
answered on 01 Jul 2013, 04:06 PM
Hello,

 We have introduced built-in keyboard navigation that executes the CommitEdit command, when "Enter" is pressed. You can override its default behavior, by using a custom command provider. Here is an article that sheds some more light on the matter.

Regards,
Ivan Ivanov
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for SILVERLIGHT.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
0
Thnghi
Top achievements
Rank 1
answered on 27 Aug 2013, 08:10 AM
Hi all,

I have the same issue,
I have a form with Description field:

 <controls:DataFormDataField.ContentTemplate>
                                            <DataTemplate>
                                                <TextBox TextWrapping="Wrap" Text="{Binding Description, Mode=TwoWay}" VerticalScrollBarVisibility="Auto"  AcceptsReturn="True" MaxHeight="100" MinHeight="40"
                                                         KeyDown="UIElement_OnKeyDown"/>
                                            </DataTemplate>
             </controls:DataFormDataField.ContentTemplate>


I try to use CustomKeyboardCommandProvider:

 public override List<DelegateCommandWrapper> ProvideCommandsForKey(KeyEventArgs args)
        {
            List<DelegateCommandWrapper> actionsToExecute = base.ProvideCommandsForKey(args);
            if (args.Key == Key.Enter)
            {
                actionsToExecute.Clear();

            }

            return actionsToExecute;
        }


but it does not close form only, and the Description field does not add new line as expected.

Has any one fixed this issue, please help me

Thanks you very much

0
jen
Top achievements
Rank 1
answered on 27 Aug 2013, 12:24 PM
I used the custom command provider to determine if the original source is a textbox that accepts returns. Hope this helps!

        public override List<DelegateCommandWrapper> ProvideCommandsForKey(KeyEventArgs args)
        {
            List<DelegateCommandWrapper> actionsToExecute = base.ProvideCommandsForKey(args);

            if (args.Key == Key.Enter)
            {
              
                    if (args.OriginalSource is TextBox && (args.OriginalSource as TextBox).AcceptsReturn)
                    {
                        TextBox TB = args.OriginalSource as TextBox;

                        actionsToExecute.Clear();

                        StringBuilder SB = new StringBuilder(TB.Text);
                        TB.Text = SB.AppendLine().ToString(); //add new line

                        TB.SelectionStart = TB.Text.Length; //move cursor to end
                        TB.Focus();
                    }
                    else
                    {
                        actionsToExecute.Clear();
                        actionsToExecute.Add(new DataFormDelegateCommandWrapper(RadDataFormCommands.CommitEdit, this.DataForm));
                    }
              
            }
         
            ...
           
            return actionsToExecute;
        }

0
Thnghi
Top achievements
Rank 1
answered on 28 Aug 2013, 04:52 AM
Hi Jen,

Thanks you very about your help, it works wonderfully, :)

Hope you have a nice day.
0
Rami Abughazaleh
Top achievements
Rank 1
answered on 01 Apr 2015, 09:06 PM
Thank you jen for the sample code.

The only issue I found is that pressing the enter key in-between text within the textbox does not add a new line at the cursor position, but will always append a new line at the end of all characters in the textbox which is unexpected.
0
Rami Abughazaleh
Top achievements
Rank 1
answered on 01 Apr 2015, 09:17 PM
I've updated jen's code to resolve the issue as follows:

        public override List<DelegateCommandWrapper> ProvideCommandsForKey(KeyEventArgs args)
        {
            List<DelegateCommandWrapper> actionsToExecute = base.ProvideCommandsForKey(args);

            if (args.Key == Key.Enter)
            {
                if (args.OriginalSource is TextBox && (args.OriginalSource as TextBox).AcceptsReturn)
                {
                    TextBox textBox = args.OriginalSource as TextBox;

                    actionsToExecute.Clear();

                    StringBuilder stringBuilder = new StringBuilder(textBox.Text);
                    stringBuilder.Insert(textBox.SelectionStart, Environment.NewLine); // add new line
                    textBox.Text = stringBuilder.ToString();

                    textBox.SelectionStart += 3; // move cursor to new line
                    textBox.Focus();
                }
                else
                {
                    actionsToExecute.Clear();
                    actionsToExecute.Add(new DataFormDelegateCommandWrapper(RadDataFormCommands.CommitEdit, this.DataForm));
                }
            }

            return actionsToExecute;
        }
Tags
DataForm
Asked by
jen
Top achievements
Rank 1
Answers by
Ivan Ivanov
Telerik team
Thnghi
Top achievements
Rank 1
jen
Top achievements
Rank 1
Rami Abughazaleh
Top achievements
Rank 1
Share this question
or