简体   繁体   中英

How to Bind checkbox to class object in List of items in datagrid wpf

I have a datagrid that is bound to a list of items which contains the itemsource. The checkbox control is a column on the grid and when checked will store specific informatoion when a button is clicked. My problem is that I cannot get the Object checkBox to bind to the XAML checkbox and when I check the checkbox it is giving me a null value or false.

XAML for Checkbox:

<DataGridTemplateColumn Header="Ist aktiv">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <CheckBox IsChecked="{Binding Path= checkBox }" Name="checkBox" IsEnabled="{Binding Enabled}"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn

>

Button Event:

private void checkinCheckBox() {


                List<AwaitingMeds> awaitingMedslist = MedicationDatagrid.ItemsSource as List<AwaitingMeds>;
                //List<AwaitingMeds> awaitingMedslistSelected = new List<AwaitingMeds>();


                List<AwaitingMeds> awaitingMedslistSelected = ( from m in awaitingMedslist
                                                                where m.checkBox.Equals( true )
                                                                select m ).ToList<AwaitingMeds>();


                foreach( AwaitingMeds checkedItems in awaitingMedslistSelected )
                  {

                      MedicationDispenseData.UpdateMedicationDispense( checkedItems.MedicationDispenseID, ( int )Enumerations.MedicationDispenseStatus.CheckedIn,false, "" );
                      MedicationDispenseHistory medicationDispenseHistory = new MedicationDispenseHistory();
                      medicationDispenseHistory.MedicationDispenseID = checkedItems.MedicationDispenseID;
                      medicationDispenseHistory.StaffID = GlobalVariables.loggedInStaff.StaffID;
                      medicationDispenseHistory.MedicationDispenseStatusID = ( int )Enumerations.MedicationDispenseStatus.CheckedIn;
                      MedicationDispenseHistoryData.Create( medicationDispenseHistory );

                  }

            }

            public IEnumerable<DataGridRow> GetDataGridRows(DataGrid grid )
            {
                var itemsSource = grid.ItemsSource as System.Collections.IEnumerable;
                if( null == itemsSource ) yield return null;
                foreach( var item in itemsSource )
                {
                    var row = grid.ItemContainerGenerator.ContainerFromItem( item ) as DataGridRow;
                    if( null != row ) yield return row;
                }
            }

your Checkbox is binded to Enabled so you should check for m.Enabled instead of m.checkbox

                List<AwaitingMeds> awaitingMedslistSelected = ( from m in awaitingMedslist
                                                            where m.**Enabled**.Equals( true )
                                                            select m ).ToList<AwaitingMeds>();

I've had the same problem.

You can't access to a control in a DataTemplate by name. Imagine what will happen if your DataGrid has 1000 rows! There will be 1000 " checkbox "es!!!!!

So try:

<CheckBox IsChecked="{Binding PropertyName,RelativeSource={RelativeSource Self}}" />

Note: Your naming is very misleading, so I might be misunderstanding what you really like to do. If so, please leave a comment and edit the question!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM