伙计我想将多行文本输入到DataGridTextColumn中,我可以使用"enter"输入多行字符.但我想像visual studio资源管理器一样使用"shift + enter",这里是我的代码,带有"enter"键.
<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>
Run Code Online (Sandbox Code Playgroud)
我用CellEditingTemplate写了一个DataGrid的usercontrol.此编辑模板的DataTemplate是一个TextBox,光标将通过三次单击进入文本框,如果我想通过双击或单击将光标设置在文本框上,我该怎么办?
这是我的代码:
<Window x:Class="MultiLineEditDataGrid.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MultiLineEditDataGrid"
Title="MainWindow" Height="350" Width="525">
<Grid DataContext="{Binding Source={x:Static Application.Current}, Path=CompanyManager}">
<Grid.RowDefinitions>
<RowDefinition Height="270"/>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<DataGrid ItemsSource="{Binding Companies}" CanUserAddRows="False" AutoGenerateColumns="False">
<DataGrid.Resources>
<DataTemplate x:Key="cellTemplate">
<TextBlock Text="{Binding Description}"/>
</DataTemplate>
<DataTemplate x:Key="cellEditingTemplate">
<local:MultiLineTextBox Text="{Binding Description}"/>
</DataTemplate>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Header="Company" Binding="{Binding Name}"/>
<DataGridTemplateColumn Header="Description"
CellTemplate="{StaticResource cellTemplate}"
CellEditingTemplate="{StaticResource cellEditingTemplate}"/>
</DataGrid.Columns>
</DataGrid>
<Button Grid.Row="1" Content="Add" Command="{Binding AddCommand}"/>
</Grid>
Run Code Online (Sandbox Code Playgroud)
MultiLineTextBox是我从文本框继承的TextBox,并重写OnKeyDown方法.
MultiLineTextBox的代码:
public class MultiLineTextBox : TextBox
{
/// <summary>
/// On Key Down.
/// </summary>
/// <param name="e"></param>
protected override …Run Code Online (Sandbox Code Playgroud) 我编写了一个片段来通过c#代码创建自己的DataTemplate.然后我将它添加到datagrid列的编辑模板中.当我调用时object templateContent = tc.CellTemplate.LoadContent ( );,应用程序崩溃,并抛出一个异常,"FrameworkElementFactory必须在此操作的密封模板中.".这是我创建datatemplate的代码.
public override DataTemplate GenerateCellTemplate ( string propertyName )
{
DataTemplate template = new DataTemplate ( );
var textBlockName = string.Format ( "{0}_TextBlock", propertyName );
FrameworkElementFactory textBoxElement = new FrameworkElementFactory ( typeof ( TextBlock ), textBlockName );
textBoxElement.SetBinding ( TextBlock.TextProperty, new Binding ( propertyName ) );
template.VisualTree = textBoxElement;
Trigger trigger = new Trigger ( );
return template;
}
Run Code Online (Sandbox Code Playgroud) wpf exception datatemplate wpfdatagrid frameworkelementfactory
wpf ×3
datagrid ×2
cursor ×1
datatemplate ×1
exception ×1
focus ×1
input ×1
multiline ×1
string ×1
wpfdatagrid ×1