An example of WPF data binding using OO COBOL

1 Likes
over 6 years ago

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

Comment List
Anonymous
Related Discussions
Recommended