Back to Devexpress

FunctionBindingBehavior Class

wpf-devexpress-dot-mvvm-dot-ui-0a358a81.md

latest7.8 KB
Original Source

FunctionBindingBehavior Class

Binds a ViewModel function’s result to a control’s property.

Namespace : DevExpress.Mvvm.UI

Assembly : DevExpress.Xpf.Core.v25.2.dll

NuGet Package : DevExpress.Wpf.Core

Declaration

csharp
public class FunctionBindingBehavior :
    FunctionBindingBehaviorBase
vb
Public Class FunctionBindingBehavior
    Inherits FunctionBindingBehaviorBase

Remarks

Specify the following FunctionBindingBehavior properties to bind a ViewModel function’s result to a property of a control in a View:

Target

An object whose Property value should be changed to the bound function‘s result. The default target object is the behavior’s associated object.

In the code sample below, the ChartControl’s FunnelSeries2D series is used as Target.

Property

A Target object’s property name. When the Function is executed, its result changes the specified property’s value.

In the code sample below, the FunnelSeries2D shows data specified in the DataSource property.

Source

An object whose Function‘s result should change the Target object’s Property. The default source object is an object from the associated object’s data context.

In the code sample below, the source function is defined in the ViewModel (the Source property is bound to the ViewModel).

Function

A name of a Source‘s function whose result should change the Target‘s Property value.

In the code sample below, the Function is set to the GetFilteredItems function from the bound ViewModel.

Arg* Properties

A View’s properties that the behavior should pass to the Function as arguments. You can specify up to 15 arguments.

The FunctionBindingBehavior converts the specified arguments to the parameterized type, if possible.

In the code sample below, the FunctionBindingBehavior passes the range control’s SelectionRangeStart and SelectionRangeEnd properties to the target function as arguments.

The following figure illustrates the FunctionBindingBehavior ‘s properties structure:

The following code sample binds the ChartControl to a ViewModel’s GetFilteredItems function and passes the RangeControl‘s SelectionRangeStart and SelectionRangeEnd properties:

xaml
<dxc:ChartControl ... >
    <dxc:ChartControl.Diagram>
        <dxc:SimpleDiagram2D>
            <dxc:SimpleDiagram2D.Series>
                <dxc:FunnelSeries2D x:Name="Series" ... >
                    ...
                </dxc:FunnelSeries2D>
            </dxc:SimpleDiagram2D.Series>
        </dxc:SimpleDiagram2D>
    </dxc:ChartControl.Diagram>
    ...
    <dxmvvm:Interaction.Behaviors>
        <dxmvvm:FunctionBindingBehavior Target="{Binding ElementName=Series}"       
                                        Property="DataSource" 
                                        Source="{Binding}"
                                        Function="GetFilteredItems" 
                                        Arg1="{Binding SelectionRangeStart, ElementName=rangeControl}" 
                                        Arg2="{Binding SelectionRangeEnd, ElementName=rangeControl}"/>
        </dxmvvm:Interaction.Behaviors>
</dxc:ChartControl>
<dxe:RangeControl Name="rangeControl" SelectionRangeStart="1/1/2015" SelectionRangeEnd="7/1/2015">
    <!-- ... -->
</dxe:RangeControl>
csharp
public class MainViewModel {
    protected MainViewModel() { ... }

    public static MainViewModel Create() {
        return ViewModelSource.Create(()=>new MainViewModel());
    }

    public virtual ObservableCollection<DataItem> Points { get; set; }

    public IList<DataItem> GetFilteredItems(DateTime start, DateTime end) {
        return this.Points.Where(x => x.Date.Date >= start && x.Date.Date <= end).ToList();
    }
}
vb
Public Class MainViewModel
    Protected Sub New()
        ...
    End Sub

    Public Shared Function Create() As MainViewModel
        Return ViewModelSource.Create(Function() New MainViewModel())
    End Function

    Public Overridable Property Points() As ObservableCollection(Of DataItem)

    Public Function GetFilteredItems(ByVal start As Date, ByVal [end] As Date) As IList(Of DataItem)
        Return Me.Points.Where(Function(x) x.Date.Date >= start AndAlso x.Date.Date <= [end]).ToList()
    End Function
End Class

View Example: How to Use FunctionBindingBehavior

Re-Invoke a Function

The FunctionBindingBehavior re-invokes the specified Function when one of its arguments is changed.

You can execute the POCOViewModelExtensions.UpdateFunctionBinding extension method to re-invoke the GetFilteredItems function:

csharp
public class MainViewModel {
    ...
    public void Update() {
        this.UpdateFunctionBinding(x => x.GetFilteredItems(default(DateTime), default(DateTime)));
    }
}
vb
Public Class MainViewModel
    ...
    Public Sub Update()
        Me.UpdateFunctionBinding(Function(x) x.GetFilteredItems(Nothing, Nothing))
    End Sub
End Class

Inheritance

Object DispatcherObject DependencyObject Freezable Animatable DevExpress.Mvvm.UI.Interactivity.AttachableObjectBase DevExpress.Mvvm.UI.Interactivity.Behavior DevExpress.Mvvm.UI.Interactivity.Behavior<DependencyObject> FunctionBindingBehaviorBase FunctionBindingBehavior

See Also

FunctionBindingBehavior Members

DevExpress.Mvvm.UI Namespace