Having problems with your account or logging in?
A lot of changes are happening in the community right now. Some may affect you. READ MORE HERE

An example of WPF data binding using OO COBOL

An example of WPF data binding using OO COBOL

This WPF application allows us to edit a person’s first and last name. The application then displays the full name formatted as <LastName>, <FirstName>. The formatted name get immediately updated whenever the first or last name changes.

Most of the data binding scenarios in WPF use data binding in XAML. Here we show an example of binding using OO COBOL.

Have the WPF Window's constructor call the below method:

method-id BindInCode.
local-storage section.
01 object1 type Person.
01 binding type Binding.
procedure division.

    set object1 to new Person
    set object1::FirstName to "Josh"
    set object1::LastName  to "Smith"
    set binding to new Binding()
    set binding::Source to object1
    set binding::UpdateSourceTrigger to type UpdateSourceTrigger::PropertyChanged
    set binding::Path to new PropertyPath("FirstName")
    invoke firstNameTextBox::SetBinding(type TextBox::TextProperty, binding)

   
set binding to new Binding()
    set binding::Source to object1
    set binding::UpdateSourceTrigger to type UpdateSourceTrigger::PropertyChanged
    set binding::Path to new PropertyPath("LastName")
    invoke lastNameTextBox::SetBinding(type TextBox::TextProperty, binding)

    set binding to new Binding()
    set binding::Source to object1           
    set binding::Path to new PropertyPath("FullName")           
    invoke fullNameTextBlock::SetBinding(type TextBlock::TextProperty, binding)

end method.

The Person class implements the INotifyPropertyChanged interface to raise a PropertyChanged event for the FullName property when the FirstName or LastName properties are set.

class-id WPFBinding.Person implements type INotifyPropertyChanged.
working-storage section.
01 ws-firstname String.
01 ws-lastname  String.
01 PropertyChanged type PropertyChangedEventHandler event public.     

property-id FirstName String.

     getter.
        set property-value to ws-firstname
    setter.
        set ws-firstname to property-value
        invoke self::OnPropertyChanged("FullName")

end property.

property-id LastName String.

    getter.
        set property-value to ws-lastname
    setter.
        set ws-lastname to property-value
        invoke self::OnPropertyChanged("FullName")

end property.

property-id FullName String.

    getter.
        set property-value to String::Format("{0},{1}", self::LastName, self::FirstName)

end property.    

method-id OnPropertyChanged.
Procedure division using propName as string.

    if self::PropertyChanged not equal to null
        invoke self::PropertyChanged(self, new PropertyChangedEventArgs(propName))
    end-if

end method.

end class.

The complete solution project is attached.

WPFBinding.zip

DISCLAIMER:

Some content on Community Tips & Information pages is not officially supported by Micro Focus. Please refer to our Terms of Use for more detail.
Top Contributors
Version history
Revision #:
1 of 1
Last update:
‎2015-04-23 14:33
Updated by:
 
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.