Data Binding Using the PropertyBridge Class
The PropertyBridge class exposes two dependency properties, Source and Target, of the System.Object type, and keeps these property values equal. In other words, when the value of one property is changed, the other property is set to the same value. This simple behavior allows you to use non-DependencyProperty properties along with WPF mechanisms that are designed to work with DependencyProperty-only properties. The following examples show how the PropertyBridge class can be used:
•
Binding between two non-dependency properties:
Assign the Source
property with a TwoWay binding having one non-dependency property as a source,
and assign the Target
property with a TwoWay binding having another non-dependency property as a
source. Then the non-dependency properties will behave as bound properties. This
will work well only in classes where exposing these non-dependency properties
supports the INotifyPropertyChanged interfaces, which is the case for
most classes from the C1ScheduleStorage
object model.
•
Setting a non-dependency property value from with a
Trigger:
Assign the Source
property with a TwoWay or OneWayToSource binding having one non-dependency
property as a source, and using Trigger's Setter to set a value to the Target
property of PropertyBridge
– this value will be assigned to the non-dependency property which is bound to
Source.
•
Many-to-many binding:
Assign Source
and Target
with MultiBinding bindings to get many-to-many binding.
•
Assign a value to a nested property:
Set the target to a TwoWay
or OneWayToSource binding with a Path referencing a nested property. Then,
assign Source
(directly or from within a Setter). The nested property will be assigned to this
value.
•
Assign the property of an object that is not accessible
directly:
Similar to assigning a value to a nested property, using
RelativeSource in binding to Target,
assign a property value for an element that can't be referenced directly in
XAML, for example, TemplatedParent or some parent element in the visual
tree.
The PropertyBridge class is derived from FrameworkElement and in order to work properly, it should be placed somewhere in the visual tree among other elements that it should communicate with. The derivation from FrameworkElement is intentional; it allows the PropertyBridge to be a part of a visual tree, which in turn provides bindings established on its properties with the correct context. For example, another theoretical option is to have PropertyBridge in the ResourceDictionary, but in this case, the bindings would be inoperable.
The PropertyBridge.Visibility property is set to Collapsed by default, so this object will not appear on a screen and doesn't participate in layout measurement and arrangement processes; it doesn't corrupt a visual representation of the visual tree where it is placed.
|