How to create a dependency property

Posted by rbf on 09-Jul-2009 10:13

Hi there,

I have created a number of user controls that I want to bind. Therefore I have created a Value property but when I am trying to bind it I get the following error message at runtime:

"Cannot bind the property 'Value' on the target control"

I have done some reading and http://msdn.microsoft.com/en-us/library/system.windows.data.binding.aspx states that the property used for binding must be a 'dependency property' (http://msdn.microsoft.com/en-us/library/ms752914.aspx).

This is where I got lost. There is no mention of 'dependency properties' in the ABL docs. How do I create this? Do I really need this or is there another way to support data binding on user controls?

Thanks for any insight!

All Replies

Posted by Peter Judge on 09-Jul-2009 10:25

rbf wrote:

Hi there,

I have created a number of user controls that I want to bind. Therefore I have created a Value property but when I am trying to bind it I get the following error message at runtime:

"Cannot bind the property 'Value' on the target control"

I have done some reading and http://msdn.microsoft.com/en-us/library/system.windows.data.binding.aspx states that the property used for binding must be a 'dependency property' (http://msdn.microsoft.com/en-us/library/ms752914.aspx).

This is where I got lost. There is no mention of 'dependency properties' in the ABL docs. How do I create this? Do I really need this or is there another way to support data binding on user controls?

Thanks for any insight!

Try using System.Windows.Forms.Binding instead of System.Windows.Data.Binding. It looks like the latter is for WPF.

-- peter

Posted by rbf on 09-Jul-2009 11:05

I *am* using System.Windows.Forms.Binding and may have drifted in the wrong direction looking for solutions but the error message does not give me a reason why it does not work.

Posted by Admin on 09-Jul-2009 11:18

Hi Peter,

have you inherited from a .NET Control and created a custom property called Value?

I'm not sure if this property is visible to the .NET side. That would require that the .NET reflection would be able to see the ABL properties.

Mike

Posted by rbf on 09-Jul-2009 12:16

It is a user control (inherited from Progress.Windows.UserControl) and then I created a Value property.

So has anyone ever succeeded in binding a user control or not?

Posted by jquerijero on 09-Jul-2009 13:44

You need a .NET based UserControl created in Visual Studio or similar.

Posted by Peter Judge on 09-Jul-2009 13:56

I have created a number of user controls that I want to bind. Therefore I have created a Value property but when I am trying to bind it I get the following error message at runtime:

"Cannot bind the property 'Value' on the target control"

If you look at the Properties view for the User Control, at the (DataBindings) Collection, you'll see that there's only a Tag item available for binding. So you need to add your Value property to that somehow. My understanding is that this requires a DependencyProperty.

From MSDN:

http://msdn.microsoft.com/en-us/library/ms752347.aspx

http://msdn.microsoft.com/en-us/library/ms750428.aspx

And also useful:

http://techpunch.wordpress.com/2008/09/25/wpf-wf-what-is-a-dependency-property/

Following those, I tried to create one in the ABL. However, because of a bug, I didn't get especially far.

For those feeling frisky, here are my steps:

First thing, you need to "Add Assembly Reference" of WindowsBase to the project.

The code below fails because GetType() returns null for "ucItems" in the static constructor. I've logged this as a bug, and revisit once that's resolved.

class ucItems inherits UserControl   :
   
    def static public property ValueProperty as System.Windows.DependencyProperty no-undo get. private set.
    def public property MyValue as "System.String[]" no-undo get. set.
   
    constructor static ucItems ():
        ucItems:ValueProperty = DependencyProperty:Register(
                "MyValue",
                Progress.Util.TypeHelper:GetType('System.String[]'),
                Progress.Util.TypeHelper:GetType('ucItems')).
    end constructor.

Note also that MyValue needs to have a .NET type - and the ABL disallows System.String or any other ABL primitive datatype - so there's a decent chance that this may not work at all. But it's fun trying to figure it out anyway

Can you use the UserControl:Tag property (as a workaround)? Or do you want to bind multiple properties?

As mentioned elsewhere, another alternative is creating a UC in .NET.

-- peter

Posted by rbf on 09-Jul-2009 14:04

Ouch! Seems that I have opened another can of worms here.

I will try using the Tag property and report back if that works.

Should I log  additional bugs for this?

Posted by Peter Judge on 09-Jul-2009 14:04

so there's a decent chance that this may not work at all. But it's fun trying to figure it out anyway 

Looks like the "chance that this may not work at all" is very good - probably close to 100% since this DependencyProperty stuff is WorkflowFoundation (not WinForms as I mis-read and mis-understood).

A "pure" .NET UserControl seems to be the right way to go.

Sorry for any confusion,

-- peter

Posted by rbf on 09-Jul-2009 14:14

Thanks for figuring this out.

Posted by rbf on 13-Jul-2009 06:20

OK I looked into this and the Tag property does not seem to help me any further. How do I intercept the TagChanged event (I suppose that is exactly what a dependency property should give me)?

Context: I have created user controls (among other things) for ultraTextEditor, ultraCurrencyEditor and ultraCheckEditor. All of these controls have a different way of binding (respectively the Text, Value and CheckedValue properties if I am correct). I want to hide these different implementations to the outside world (have heard rumours of 'encapsulation') because I want to bind all controls generically.

That is why I am pursuing this, but maybe I am on the wrong track? Any insights are appreciated. 

Posted by rbf on 13-Jul-2009 08:35

OK I solved this by doing it the other way around: the form performs Bind() in the user control passing in the form's BindingSource.

But I am still puzzled by 'dependency properties' and would prefer to be able to bind that way in the future.

Is this an enhancement request or a bug?

Posted by jquerijero on 13-Jul-2009 08:50

Dependency property is intended for declarative usage in XAML. It's WPF centric.

Posted by rbf on 13-Jul-2009 09:02

jquerijero wrote:

Dependency property is intended for declarative usage in XAML. It's WPF centric.

Fair enough. So how do you create a proper property for data binding in OpenEdge GUI for .NET?

Posted by rbf on 13-Jul-2009 09:06

jquerijero wrote:

Dependency property is intended for declarative usage in XAML. It's WPF centric.


Fair enough. So how do you create a proper property for data binding in OpenEdge GUI for .NET?

Posted by jquerijero on 13-Jul-2009 09:07

Like someone pointed out, bindable property requires the reflection capability of the .NET Framework. The .NET Framework doesn't know anything about ABL classes. Reflection allows the framework to inspect the class by using the its Type definition.

1. You can create a Class Library of UserControls in Visual Studio and reference it in GUI for .NET.

2. Enhancement feature like a new Helper class might be in order.

Posted by Admin on 13-Jul-2009 09:11

In C# part of the job is to add the following annotation to the property in source code:

[System.ComponentModel.Bindable(true)] 

I'm pretty sure, that's not possible from the ABL.

Posted by rbf on 13-Jul-2009 09:23

mikefe wrote:

In C# part of the job is to add the following annotation to the property in source code:

[System.ComponentModel.Bindable(true)] 



I'm pretty sure, that's not possible from the ABL.


Should we ask for that feature in the future?

Or is that undoable?

Or should I achieve it differently anyway?

Maybe Shelly should kick in at this time.

Mike I know you have created field level user controls as well, how do you do it?

-peter

Posted by jquerijero on 13-Jul-2009 09:33

I did start a discussion with Progress about .NET attributes awhile back. I did not pursue the topic any further as the scope of the project I'm working on has changed. Progress asked me create some documentation or sample(could be C#) where .NET attribute is essential. I was needing it mostly for the Serialization classes in .NET which also require attribute declaration.

Posted by Admin on 14-Jul-2009 09:41

Mike I know you have created field level user controls as well, how do you do it?

-peter

Hi Peter,

well with Dynamics4.NET I have encapsulated almost all field level and frame level controls to mimic classic ABL widget behaviour as good as possible/as much as required. But that's part of a larger Infrastructure with it's own event handling and data binding strategy (compatible to the Dynamics legacy).

For all our other development effords (like our SmartComponent Library) we have gone away from encapsulation of individual input control in UserControls (we continue to use UserControls when creating an aggregation or composition of multiple controls). Encapsulation is curse and blessing at the same time. As long as we don't have event handling in the ABL this encapsulation is a pain in the a... when you need to subscribe a large number of individual events in the Visual Designer (like Entry, Leave, Value Changed, EditorButtonClick, ...). Also you will very often need access to a larger number of properties to have the Control perform as you'd like to (think of Input mask, Editor Buttons etc). Very often Infragistics offers specialized dialogs to maintain these properties. I consider it a major advantage to have access to all these. As long as we can't define these properies that would required to enhance the property sheet of an UserControl, I'll stick with Infragistics Controls or Inherited Controls (they allow me to validate certain settings as well).

What we do to define standards is that we generate the controls from the data source within the Visual Designer. We have built our own wizards for that purpose and that allows us to ensure that all editor controls (for a certain data-type) look similar and have the right data binding properties defined.

Best regards,

Mike

Posted by Shelley Chase on 14-Jul-2009 10:03

Great discussion. Annotations / Attributes are on our GUI for .NET and OO roadmaps.If you have specific use cases (like the ones defined here), please log an enhancement request so we can consider it when planning for the next release.

This thread is closed