Click here to Skip to main content
15,391,941 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have already created the WPF form with the datagrid and checkboxes appear. The problem I think I'm missing code for is once the information is populated and checkboxes are next to each item the user should be able to select/check or uncheck the boxes but when clicking on them nothing happens.
XAML
<DataGrid x:Name="dgGrp1" ItemsSource="{Binding}" HorizontalAlignment="Left" Height="292" Margin="94,85,0,0" VerticalAlignment="Top" Width="219">
            <DataGrid.Columns>
                <DataGridCheckBoxColumn Header="Groups" Binding="{Binding CopyGrps}"  />
            </DataGrid.Columns>
        </DataGrid>


What I have tried:

I'm using visual studio 2019 to create the WPF form and really new to all this. I've read many articles as originally the VS created the column with $null values which broke the entire application I've since fixed this with using the code above which I manually modified from reading articles.

I think the other confusing part for myself is I find many things related to C# but not directly xaml code for making WPF datagrid checkbox columns and the controls. There are a few articles I reviewed on here and they have broken links
Posted
Updated 22-Jul-22 9:50am

1 solution

It is hard to tell what is happening as you do not provide you data model. Data Binding has requirements that may not be met.

Here is an introduction to the DataGrid: The DataGrid control - The complete WPF tutorial[^]. The Second page shows binding controls to the data. Additional information on the control is also included that may be helpful for you.

As for Data Binding, here is a good tutorial: Introduction to WPF data binding - The complete WPF tutorial[^]

UPDATE
Following up the question below, here is a simple working example:

1. Data Model = WidgetModel
C#
public class WidgetModel
{
    public string Name { get; set; }
    public bool IsChecked { get; set; }
}

2. Simple behind View data initializing and data binding:
C#
public partial class MainWindow : Window
{
    public ObservableCollection<WidgetModel> Widgets { get; init; }

    public MainWindow()
    {
        InitializeComponent();

        Widgets = new ObservableCollection<WidgetModel>
        {
            new WidgetModel { Name = "Item 1", IsChecked = true},
            new WidgetModel { Name = "Item 2"}
        };

        DataContext = this;
    }
}

3. Finally the View itself:
XML
<DataGrid ItemsSource="{Binding Widgets}">
</DataGrid>


The only difference between my example and the example in the links provided above is that I set the DataContext for the View (Window) and point the ItemSource to the collection in the View.

This is a working example. It is also the bare minimum to making it work.

Again, all is provided in the links above. The rest is up to you.
   
v2
Comments
Member 15352875 27-May-22 16:45pm
   
@Graeme_grant what other information do I need to provide to better answer my question? You said I did not provide a data model not sure what your looking for more information from VS2019?

I have a WPF form with 2 Data grid boxes that each contain 1 checkbox column.

I'm unable to check the boxes when the application is running. I want the user to be able to check or uncheck the boxes. I'm not sure what is missing. I'm using VS 2019 to build the WPF form using XAML code to add to my PowerShell application.
Graeme_Grant 27-May-22 20:49pm
   
Did you actually look at the links provided? i'll provide a simple example in my updated answer.

Update: As you can see from my updated answer, I provide all parts to reproduce the working solution. When asking questions, you need to provide all parts so we have nothing to guess as we can not see your screen.

That website that I provided in my initial answer is a fantastic resource that should not be ignored.
Member 15352875 27-May-22 22:59pm
   
Okay thanks Graeme_Grant. I don't understand how I would use the C# language with VS2019 that uses XAML code which I save the mainwindow.xaml and then import it with powershell. So not sure this is what I'm looking for but thanks for your time anyways.
Graeme_Grant 28-May-22 6:25am
   
Powershell? You have lost me... that wasn't in your question. You require some form of code to handle the collection used by the DataGrid, either C# or VB. Without it, it will not work.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900