The Nuts and Bolts of Property Binding

[Previous] [Next]

Before I describe the specifics of the Real-Time Stock Portfolio solution, I want to describe the general property-binding mechanism used by the Spreadsheet component. The Spreadsheet component uses the standard COM mechanism for property binding, known as the IPropertyNotifySink interface. This interface can be implemented by any component that wants to know when a property of a COM object has changed. The COM object calls methods on this interface to let the listening component (in this case, the Spreadsheet component) know when a bindable property value has changed. Any time the Spreadsheet component hears that a property value has changed, it asks the COM object for the new value, sets the bound cell's value to the new property value, and recalculates any dependent cells. Figure 10-1 diagrams the whole process.

click to view at full size.

Figure 10-1. The COM property-binding architecture.

Technically, the COM standard requires a source object to let a listening object know about changes to properties only if the source object has marked those properties as bindable, though many source objects will notify the listener when any property changes. You can mark object properties as bindable by including the bindable keyword in the property's declaration in your type library. If you build an object in C++, the relevant part of your IDL file will look like this:

 interface _Class1 : IDispatch {     [id(0x68030000), propget, bindable]     HRESULT BindableProp([out, retval] VARIANT* rhs); }; 

In Visual Basic, you can declare a property bindable by checking an option in the Procedure Attributes dialog box. While your class code module is open, choose the Tools|Procedure Attributes menu item and click the Advanced button to fully expand the dialog box. Select the property using the Name drop-down list at the top of the dialog box, and select the Property Is Data Bound check box. Figure 10-2 shows the Procedure Attributes dialog box and the relevant check box.

When the source object wants to indicate that a property value has changed, it calls the OnChanged method on the listening component's IPropertyNotifySink interface. Visual Basic makes this rather easy by offering the PropertyChanged method on the UserControl object or, as you will see soon, on public classes marked as simple data bound. Any time a source object tells the Spreadsheet component that a bound property has changed, the Spreadsheet component requests the property's new value, sets the new value into the bound cell, and recalculates any dependent cells. All this happens automatically, without requiring any special code.

Figure 10-2. The Procedure Attributes dialog box.

Ordinarily, the Spreadsheet control is smart enough to request new values for only those properties that have actually changed and update only the cells referencing those changed properties. However, there is one special case in which the Spreadsheet control obtains new values whenever the spreadsheet is recalculated, regardless of whether the source control has raised a property change notification. If you use parentheses anywhere in your property-binding expression, the Spreadsheet control will consider the formula volatile and will put it into a semi-calc mode. By convention, the Spreadsheet control evaluates semi-calc cells every time it recalculates any part of the model. The classic example of a semi-calc function is the NOW function, which returns the current system time.

For example, suppose you enter a property binding that looks like this:

 =document.MyControl.MyMethod(A1).MyProperty 

The Spreadsheet control will consider this formula volatile and will evaluate the formula (requesting a new property value) whenever the control recalculates any part of the spreadsheet, even if this cell has no dependency on the changed cells. Sometimes this is a desirable behavior because it always ensures that the newest possible value is in the spreadsheet. However, if obtaining the new property value is a timeconsuming operation, you will want to avoid this side effect. To circumvent this, make sure you have no parentheses in the property-binding formula. This might be difficult if your real-time source control can take a dynamic parameter, such as a stock symbol. If the possible values the source can return are limited or fixed, make the values individual properties instead of using a single method or an indexed property that requires parentheses in your expression.

Note that the Stock Ticker control in this solution does require that you use parentheses in property-binding formulas. That was somewhat unavoidable, as I wanted to let users enter any stock symbol in the left column and see real-time data to the right of that symbol. I could have designed the Stock Ticker control to fetch data for only one symbol at a time and then used script to set a CurrentSymbol property to the newly entered symbol in the spreadsheet. The downside of that approach is that it requires placing many instances of the Stock Ticker control on the page, each of which would make separate requests against the Microsoft Investor web site. This would decrease the scalability of the control. Because the control gets new values for all symbols at the same time and then caches those values until the next refresh, the semi-calc behavior is not inconvenient in this solution.

The key aspect of the Spreadsheet control's property-binding feature in relation to real-time data is that the control continues to listen for and process property change notifications even while the user edits cell formulas or applies formatting. Users can continue to augment their spreadsheets as new values flow into the model.

Note that the spreadsheet can establish property bindings only when it is hosted in Internet Explorer. You cannot use the property-binding feature in other containers.



Programming Microsoft Office 2000 Web Components
Programming Microsoft Office 2000 Web Components (Microsoft Progamming Series)
ISBN: 073560794X
EAN: 2147483647
Year: 1999
Pages: 111
Authors: Dave Stearns

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net