guys I want to input multi-line text into a DataGridTextColumn, I can use "enter" to input a multi-line character. But i want use "shift+enter" like visual studio resource manager, here is my code with "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>
One way you can do this is by handling the KeyDown event on the TextBox using an EventSetter in your Style. I took your example, removed the AcceptsReturn setter in the style, and added a KeyDown handler to the EditingElementStyle that adds a newline to where the caret was and moves the 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 the example in the Window class from a new Application project template, so here's the C# for the whole Window with the event handling code. Note that I set Handled to true to stop the event from bubbling up anywhere as I don't want the Return key to be handled as a commit to the editing row in this case. I think this is actually one of the downsides of the approach. Stopping the bubbling/tunneling of an event is something that, if you have complex interactions with user input in your application, can easily grow into a logic bomb. But it isn't so bad if you just have one special case like this. So as with everything, use cautiously as the part of your UI using this grows.
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; }
}
One other thing to consider is that you may want the behavior to be different if the insert key has been pressed and you are in override input mode. Maybe in this case, the next character should be replaced by the new line. But the resource editor in Visual Studio 2010 doesn't seem to react to the insert key (it also doesn't show the text as multi-line). But I think given this example, you could extend it to work well with the insert key. Hope this helps - good luck!
Set TextWrapping to Wrap and AcceptsReturn = True...
<DataGridTextColumn.EditingElementStyle>
<Style TargetType="TextBox">
<Setter Property="TextWrapping" Value="Wrap" />
<Setter Property="AcceptsReturn" Value="true" />
</Style>
</DataGridTextColumn.EditingElementStyle>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With