1
votes

Here is my Employee class.

public class Employee
    {
        public string LastName { get; set; }
        public string FirstName { get; set; }
        public bool Fire { get; set; }
    }

This is how XAML is setup.

<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>

And finally here is the EmployeesGridLoaded method.

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;
    }

}

Through this grid, my goal is that last & first name columns should be read-only. I also don't want to have ability to add a new row but I want the Fire column as editable, so that I can choose which employees I want to fire. Below is what I am getting.

enter image description here

I know that I can set binding mode to One-way which will make last and first name columns non-ediable but with that I still get an extra empty row in the grid. Can you please help me solve this problem so that I can start firing employees?

1
As a side note, if you are making this action immediate, I would use a button "FIRE!" rather than a checkbox. Unless you are going to check multiple boxes then hit OK at the bottom of the dialog. - GEEF
yes i will have a button that will fire all employees where Fire column is checked - BKS
To be a bit of a stickler and to go along with what @VP. said, wouldn't the property be 'Fired' and the action (like a button) be 'fire'? - cost
why voted down? Anything wrong? - BKS

1 Answers

3
votes

The empty line is for adding new rows. You need to set this attribute in the DataGrid:

<DataGrid CanUserAddRows="False" />