这是我的员工类。
public class Employee
{
public string LastName { get; set; }
public string FirstName { get; set; }
public bool Fire { get; set; }
}
这就是XAML的设置方式。
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid Loaded="EmployeesGridLoaded">
<DataGrid x:Name="gEmployees" HorizontalAlignment="Left" Margin="10,10,0,0"
VerticalAlignment="Top" AlternatingRowBackground="LightBlue" AlternationCount="2" AutoGenerateColumns="False" Grid.Row="0">
<DataGrid.Columns>
<DataGridCheckBoxColumn Header="Fire" Binding="{Binding Fire}" Width="1*" />
<DataGridTextColumn Header="Last Name" Binding="{Binding LastName}" Width="3*" />
<DataGridTextColumn Header="First Name" Binding="{Binding FirstName}" Width="3*" />
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
最后这是EmployeesGridLoaded方法。
private void EmployeesGridLoaded(object sender,RoutedEventArgs e)
{
List<Employee> Employees = new List<Employee>()
{
new Employee() { Fire = false, LastName = "Silly", FirstName = "Dude" },
new Employee() { Fire = false, LastName = "Mean", FirstName = "Person" },
};
gEmployees.ItemsSource = Employees;
}
}
通过这个网格,我的目标是最后一个&amp;名字列应该是只读的。我也不希望能够添加新行,但我希望Fire列可编辑,以便我可以选择要触发的员工。以下是我得到的内容。
我知道我可以将绑定模式设置为One-way,这会使last和first name列不可编辑但是我仍然在网格中得到一个额外的空行。能否帮我解决这个问题,以便我可以开始解雇员工?
答案 0 :(得分:3)
空行用于添加新行。您需要在DataGrid
:
<DataGrid CanUserAddRows="False" />