WPF DataGridTextColumn multi-line input

家住魔仙堡 提交于 2019-12-10 01:10:22

问题


I am using the WPF DataGrid control in .NET 4 that have a DataGridTextColumn.

I want to be able to enter multi-line text. The line breaks are formatted correctly when I bind data to the column, but I've found no way of creating the line breaks when editing the text.

<DataGrid ItemsSource="{Binding MyMessages}">
   <DataGrid.Columns>
      <DataGridTextColumn Header="Message" Binding="{Binding Path=Message}"  Width="Auto"/>
   <DataGrid.Columns>
</DataGrid>

Any suggestions?


回答1:


Try:

<DataGridTextColumn Header="Message" Binding="{Binding Path=Message}"  Width="Auto">
        <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>
    </DataGridTextColumn>



回答2:


xmlns:wtk="clr-namespace:Xceed.Wpf.Toolkit;assembly=Xceed.Wpf.Toolkit"

<DataTemplate x:Key="dataGridMultiLineTextBoxTemplateColumn" DataType="your data type">
        <wtk:MultiLineTextEditor
            x:Name="MultiLineTextBox"
            Width="300"
            Margin="2"
            Padding="5,0,0,0"
            FontSize="12"
            FontWeight="Normal"
            Foreground="Black"
            IsSpellCheckEnabled="True"
            Text="{Binding your binding property, Mode=TwoWay, NotifyOnTargetUpdated=True, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}"
            TextWrapping="Wrap" />
    </DataTemplate>

Extended WPF Toolkit MultiLineTextEditor will provide what you need. Extended WPF Toolkit MultiLineTextEditor



来源:https://stackoverflow.com/questions/6354694/wpf-datagridtextcolumn-multi-line-input

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!