Bind to a Method : Binding « Windows Presentation Foundation « VB.Net






Bind to a Method

Bind to a Method
    
<Window 
    x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:system="clr-namespace:System;assembly=mscorlib"
  xmlns:WpfApplication1="clr-namespace:WpfApplication1"
    Title="WPF"  Width="240" Height="150" >
    <Window.Resources>
        <WpfApplication1:DoubleToString x:Key="doubleToString" />
        <ObjectDataProvider x:Key="convertDistance"
            ObjectType="{x:Type WpfApplication1:DistanceConverter }"
            MethodName="Convert" >
            <ObjectDataProvider.MethodParameters>
                <system:Double>0</system:Double>
                <WpfApplication1:DistanceType>Miles</WpfApplication1:DistanceType>
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>

    </Window.Resources>
    <StackPanel>
        <TextBlock Text="Enter a distance to convert:"/>
        <TextBox Text ="{Binding Source={StaticResource convertDistance},
                    Path=MethodParameters[0],BindsDirectlyToSource=true,UpdateSourceTrigger=PropertyChanged,
                    Converter={StaticResource doubleToString}}"/>
        
        <ComboBox Width="80" HorizontalAlignment="Left"
            SelectedValue="{Binding Source={StaticResource convertDistance},Path=MethodParameters[1], 
                            BindsDirectlyToSource=true}" >
            <WpfApplication1:DistanceType>Miles</WpfApplication1:DistanceType>
            <WpfApplication1:DistanceType>Kilometres</WpfApplication1:DistanceType>
        </ComboBox>

        <TextBlock Text="Result:"/>
        <TextBlock Text="{Binding Source={StaticResource convertDistance}}"/>
    </StackPanel>
</Window>


//File:Window.xaml.vb

Imports System
Imports System.Windows.Data

Namespace WpfApplication1
  Public Class DoubleToString
    Implements IValueConverter
    Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.Convert
      If value IsNot Nothing Then
        Return value.ToString()
      End If
      Return Nothing
    End Function

    Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
      Dim strValue As String = TryCast(value, String)
      If strValue IsNot Nothing Then
        Dim result As Double
        Dim converted As Boolean = [Double].TryParse(strValue, result)
        If converted Then
          Return result
        End If
      End If
      Return Nothing
    End Function
  End Class


  Public Enum DistanceType
    Miles
    Kilometres
  End Enum

  Public Class DistanceConverter
    Public Function Convert(amount As Double, distancetype__1 As DistanceType) As String
      If distancetype__1 = DistanceType.Miles Then
        Return (amount * 1.6).ToString("0.##") & " km"
      End If

      If distancetype__1 = DistanceType.Kilometres Then
        Return (amount * 0.6).ToString("0.##") & " m"
      End If

      Throw New ArgumentOutOfRangeException("distanceType")
    End Function
  End Class
End Namespace

   
    
    
    
  








Related examples in the same category

1.Bind to a Property of a UI ElementBind to a Property of a UI Element
2.Bind a Property of an Element to ItselfBind a Property of an Element to Itself
3.Specify a Default Value for a BindingSpecify a Default Value for a Binding
4.Create a Two-Way BindingCreate a Two-Way Binding
5.Debug Bindings Using Attached PropertiesDebug Bindings Using Attached Properties
6.String Array ResourceString Array Resource
7.Bind animate target value to Canvas(Window) widthBind animate target value to Canvas(Window) width
8.Display Current Date and Time: binding the DateTime.Now to LabelDisplay Current Date and Time: binding the DateTime.Now to Label
9.One way and two way bindingOne way and two way binding
10.Custom Element Binding WindowCustom Element Binding Window
11.Get XmlElement from Bounded viewGet XmlElement from Bounded view
12.Customize UpdateSourceExceptionFilterCustomize UpdateSourceExceptionFilter
13.Assign your own class to DataContent and bind to TextBoxAssign your own class to DataContent and bind to TextBox
14.Async bindingAsync binding
15.Null property bindingNull property binding
16.Binding Property with ExceptionBinding Property with Exception
17.Hierarchical Binding for three level nested objectsHierarchical Binding for three level nested objects
18.BindingOperations.GetBindingExpressionBindingOperations.GetBindingExpression
19.Bind to enum typesBind to enum types
20.Manual Update TargetManual Update Target
21.Bind to the Values of an EnumerationBind to the Values of an Enumeration
22.Master Detail BindingMaster Detail Binding
23.Data binding using collections composed of mixed types of data.Data binding using collections composed of mixed types of data.
24.Binding Environment InfoBinding Environment Info
25.Bind to an ADO.NETDataSetBind to an ADO.NETDataSet
26.Text Data BindingText Data Binding
27.Bind to an Existing Object InstanceBind to an Existing Object Instance
28.Digital ClockDigital Clock
29.Use Data Triggers to Change the Appearance of Bound DataUse Data Triggers to Change the Appearance of Bound Data