默认情况下,WPF datagridtext显示为标签,并在单击时进入编辑状态。有没有办法修改列,以便文本框始终可见(而不是取决于click事件)? 提前致谢, JP
答案 0 :(得分:1)
我根据您在评论中的澄清更新了我的答案。您可以自己为单元格设置模板。以下是age列使用文本块的示例。
XAML:
<Window x:Class="GridTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Controls="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit"
Height="300" Width="300">
<StackPanel>
<Controls:DataGrid Name="dataGrid" AutoGenerateColumns="False" >
<Controls:DataGrid.Columns>
<Controls:DataGridTextColumn
Header="Name"
Binding="{Binding Path=Name}" />
<Controls:DataGridTemplateColumn Header="Age">
<Controls:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=Age}" />
</DataTemplate>
</Controls:DataGridTemplateColumn.CellTemplate>
<Controls:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=Age}" />
</DataTemplate>
</Controls:DataGridTemplateColumn.CellEditingTemplate>
</Controls:DataGridTemplateColumn>
</Controls:DataGrid.Columns>
</Controls:DataGrid>
</StackPanel>
</Window>
代码背后:
using System;
using System.Collections.Generic;
using System.Windows;
namespace GridTest
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
dataGrid.ItemsSource = new List<Person>(
new Person[]
{
new Person("Bob", 30),
new Person("Sally", 24),
new Person("Joe", 17)
});
}
}
public class Person
{
public String Name { get; set; }
public int Age { get; set; }
public Person(String name, int age)
{
Name = name;
Age = age;
}
}
}