vendredi 13 juillet 2018

Why is there an extra Checkbox in the DataGrid while binding?

I am trying to create a basic DataGrid with checkbox as 1st column.

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

And here is the .cs side of it

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;       

    }

Everything works great, but when I look at the GUI there is this extra checkbox in the DataGrid. I do not understand where it comes from or how to get rid of it. I have followed so many tutorials and they all have this extra checkbox as well but nobody talks about it! Does it come by default? Is there a way I can remove it?

I have also tried the following way of generating checkboxes and yet it yields the same result.

<DataGridTemplateColumn Header="Select">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <CheckBox IsChecked="{Binding Path=Select, UpdateSourceTrigger=PropertyChanged}" />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>




Aucun commentaire:

Enregistrer un commentaire