Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF: DataGrid how can i input multi-line text

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>

like image 387
yafeya Avatar asked Feb 11 '11 11:02

yafeya


2 Answers

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!

like image 185
timmyl Avatar answered Sep 30 '22 05:09

timmyl


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>
like image 37
blins Avatar answered Sep 30 '22 05:09

blins