WPF DataGrid and CheckBoxes

Hi! 

I have a WPF datagrid in my program, which has an check box in it - 

<DataGridTemplateColumn Header="Active?" Width="100" >
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <CheckBox x:Name="PP_ACTIVE" IsChecked="{Binding XPath=@active, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

I am reading data from a file that needs to set the check box if the field in the data is set to "X".

How do I do this?  The SetAttributes command only works for String fields, and the CheckBox is a a Boolean field. 

01 PAYPLAN_DG_XDOC type System.Xml.XmlDocument value new System.Xml.XmlDocument().

01 DG_ITEM type System.Xml.XmlElement.

SET DG_ITEM TO PAYPLAN_DG_XDOC::CreateElement("item").
MOVE SQ-UB41601-PAY-PLAN-MONTH TO D-PAY-PLAN-MONTH.
MOVE SQ-UB41601-PAY-PLAN-YEAR TO D-PAY-PLAN-YEAR.
INVOKE DG_ITEM::SetAttribute("month", D-PAY-PLAN-MONTH).
INVOKE DG_ITEM::SetAttribute("year", D-PAY-PLAN-YEAR).

IF SQ-UB41601-ACTIVE-FLAG = "X"
     MOVE 1 TO WS-ACCEPT-VALUES
ELSE
     MOVE 0 TO WS-ACCEPT-VALUES
END-IF.

MOVE SQ-UB41601-DUE-AMT TO D-PAY-PLAN-AMT-DUE.
ADD SQ-UB41601-DUE-AMT TO W-PAY-PLAN-BALANCE.
INVOKE DG_ITEM::SetAttribute("due", D-PAY-PLAN-AMT-DUE).

MOVE SQ-UB41601-AMT-PAID TO D-PAY-PLAN-AMT-PAID.
ADD SQ-UB41601-AMT-PAID TO W-PAY-PLAN-PAID-BALANCE.
INVOKE DG_ITEM::SetAttribute("paid", D-PAY-PLAN-AMT-PAID).

INVOKE PAYPLAN_DG_XDOC::DocumentElement::AppendChild(DG_ITEM).

  • Verified Answer

    +1  

    Hi Amy,

    I wasn't sure if you found a solution to this yet but I think I might have one so I will post it here.

    It is possible to assign a converter class to the checkbox so that if you pass it a text value such as "0" or "1" it will convert this to a boolean true or false before binding the XML Document to the grid.

    Here is a small sample that does exactly that:

    Window1.xaml:

    <Window x:Class="testcheckbind.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:testcheckbind"
        Title="Window1" Height="350" Width="525">
        <Window.Resources><local:IntToBoolConverter x:Key="CheckBoxConv" />
        </Window.Resources>
        <Grid Name="myGrid">
            <StackPanel>
                <TextBox Text="{Binding XPath=@text1}" Height="30" Width="200" />
                <TextBox Text="{Binding XPath=@text2}" Height="30" Width="200" />
                <CheckBox IsChecked="{Binding XPath=@check1, Converter={StaticResource CheckBoxConv}}" />
            </StackPanel>
        </Grid>
    </Window>

    Window1.xaml.cbl:

          $set ilusing"System.Xml"
           class-id testcheckbind.Window1 is partial
                     inherits type System.Windows.Window.
    
           working-storage section.
           01 XmlDoc type XmlDocument.
           01 xmlElemMux type XmlElement.
           method-id NEW.
           procedure division.
               invoke self::InitializeComponent()
               set XmlDoc to new XmlDocument
               set xmlElemMux to XmlDoc::CreateElement("Hello")
               invoke xmlElemMux::SetAttribute("text1", "Chris")
               invoke xmlElemMux::SetAttribute("text2", "Test")
               invoke xmlElemMux::SetAttribute("check1", "1")
               set myGrid::DataContext to xmlElemMux
               goback.
           end method.
    
           end class.
           
           class-id testcheckbind.IntToBoolConverter
               implements type IValueConverter public.
           working-storage section.
           method-id Convert public.
           procedure division using by value #value as object
                                             targetType as type System.Type
                                             #parameter as object
                                             culture as type System.Globalization.CultureInfo
                          returning return-item as object.
    
               declare intValue as binary-long = type System.Convert::ToInt32(#value)
               if intValue not = 0
                  set return-item to true
               else
                   set return-item to false
               end-if.
               
           end method.
    
           method-id ConvertBack public.
           procedure division using by value #value as object
                                             targetType as type System.Type
                                             #parameter as object
                                             culture as type System.Globalization.CultureInfo
                          returning return-item as object.
               if #value as type Boolean = true
                   move "1" to return-item
               else
                   move "0" to return-item
               end-if
           end method.
           
           end class.
           

    Chris Glazier
    Rocket Software - Principal Technical Support Specialist
    If you found this post useful, give it a “Like” or click on "Verify Answer" under the "More" button