我正在尝试使用复选框作为第一列创建一个基本的DataGrid。
<DataGrid Name ="Box1Grid" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridCheckBoxColumn Header="Select" Binding="{Binding Path=Select}"/>
<DataGridTextColumn Header="Name" Binding="{Binding Name}" />
<DataGridTextColumn Header="Description" Binding="{Binding Description}" />
</DataGrid.Columns>
</DataGrid>
这是它的.cs面
public class Param : INotifyPropertyChanged
{
private bool isParamSelected;
public bool Select
{
get { return isParamSelected; }
set
{
isParamSelected = value;
NotifyPropertyChanged();
}
}
public string Name { get; set; }
public string Description { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
public TestWindow()
{
InitializeComponent();
Box1List.Add(new Param() { Name = "BAG", Description = "Input" });
Box1List.Add(new Param() { Name = "BAP", Description = "Output" });
Box1List.Add(new Param() { Name = "SAG", Description = "Input" });
Box1List.Add(new Param() { Name = "SAP", Description = "Output" });
Box1Grid.ItemsSource = Box1List;
}
一切正常,但是当我查看GUI时,DataGrid中有一个额外的复选框。我不知道它来自哪里或如何摆脱它。我已经看了很多教程,他们都有这个额外的复选框,但是没人谈论!是否默认提供?有什么方法可以删除它吗?
我还尝试了以下生成复选框的方法,但产生的结果相同。
<DataGridTemplateColumn Header="Select">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding Path=Select, UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>