WPF: DataGrid, how can I enter multiline text

guys I want to enter multiline text in a DataGridTextColumn, I can use "enter" to enter a multiline character. But I want to use "shift + enter" as the visual studio resource manager, here is my code with the "enter" key.

<DataGridTextColumn Header="Value" Binding="{Binding Value}" Width="*"> <DataGridTextColumn.ElementStyle> <Style TargetType="TextBlock"> <Setter Property="TextWrapping" Value="Wrap" /> </Style> </DataGridTextColumn.ElementStyle> <DataGridTextColumn.EditingElementStyle> <Style TargetType="TextBox"> <Setter Property="TextWrapping" Value="Wrap" /> <Setter Property="AcceptsReturn" Value="true" /> </Style> </DataGridTextColumn.EditingElementStyle> 

+8
string input multiline wpf datagrid
source share
2 answers

One way to do this is to handle a KeyDown event in a TextBox using an EventSetter in your style. I took your example, deleted the setver AcceptsReturn in style, and added a KeyDown handler to EditingElementStyle, which adds a new line to where the carriage was and moves CaretIndex to the right.

Here's the XAML:

 <DataGridTextColumn Header="Value" Binding="{Binding Value}" Width="*"> <DataGridTextColumn.ElementStyle> <Style TargetType="TextBlock"> <Setter Property="TextWrapping" Value="Wrap" /> </Style> </DataGridTextColumn.ElementStyle> <DataGridTextColumn.EditingElementStyle> <Style TargetType="TextBox"> <Setter Property="TextWrapping" Value="Wrap" /> <EventSetter Event="KeyDown" Handler="OnTextBoxKeyDown"/> </Style> </DataGridTextColumn.EditingElementStyle> </DataGridTextColumn> 

I wrote an example in the Window class from a new application project template, so here is C # for the whole window with event handling code. Note that I set Handled to true to stop the bubbling event anywhere, since I don't want the Return key to be processed in this case as a fix for the edit line. I think this is actually one of the disadvantages of the approach. Stopping bubbling / tunneling events is something that, if you have complex interactions with user input in the application, can easily turn into a logic bomb. But this is not so bad if you have only one such case. Like everyone, use caution, as part of your user interface using this is growing.

 public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); this.DataContext = new List<Thing> { new Thing { Value = "Some text" }, new Thing { Value = "More text" + Environment.NewLine + " second line" }, new Thing { Value = "Another value" } }; } private void OnTextBoxKeyDown(object sender, KeyEventArgs e) { if (Key.Return == e.Key && 0 < (ModifierKeys.Shift & e.KeyboardDevice.Modifiers)) { var tb = (TextBox)sender; var caret = tb.CaretIndex; tb.Text = tb.Text.Insert(caret, Environment.NewLine); tb.CaretIndex = caret + 1; e.Handled = true; } } } public class Thing { public string Value { get; set; } } 

Another thing to keep in mind is that you may need to have a different behavior if the insert key was pressed and you are in override input mode. Perhaps in this case, the next character should be replaced with a new line. But the resource editor in Visual Studio 2010 does not seem to respond to the insert key (it also does not display text as multi-line). But I think that given this example, you can extend it to work well with the insert key. Hope this helps. Good luck

+13
source share

Sets TextWrapping for Wrap and AcceptsReturn = True ...

 <DataGridTextColumn.EditingElementStyle> <Style TargetType="TextBox"> <Setter Property="TextWrapping" Value="Wrap" /> <Setter Property="AcceptsReturn" Value="true" /> </Style> </DataGridTextColumn.EditingElementStyle> 
+6
source share

All Articles