Ivalueconverter not working in phone 8.1 Xaml

104 Views Asked by At

I am using Case Converter in my XAML for a textbox as :

<TextBox Text="{Binding Obj.UserName,Mode=TwoWay,Converter={Binding UpperCaseConverter}}" Grid.Row="1" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Center" MinWidth="200" x:Name="usernametxtbox"/>

and converter initiated as:

 Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        UpperCaseConverter = New MCCommonCodes.Converters.CaseConverter(MCCommonCodes.Converters.CharacterCase.Lower)
        Me.DataContext = Me
    End Sub
Property UpperCaseConverter As MCCommonCodes.Converters.CaseConverter

The Code for converter is:

Public Class CaseConverter
            Implements IValueConverter
            Property SelectedCase As CharacterCase

            Public Sub New(ByVal ConvertCase As CharacterCase)
                SelectedCase = ConvertCase
            End Sub

            Public Function Convert(value As Object, targetType As Type, parameter As Object, language As String) As Object Implements IValueConverter.Convert
                Dim str As String = CType(value, String)
                If Not value = Nothing Then
                    Select Case SelectedCase
                        Case CharacterCase.Lower : Return str.ToLower
                        Case CharacterCase.Normal : Return str
                        Case CharacterCase.Upper : Return str.ToUpper
                        Case Else : Return str
                    End Select
                Else
                    Return ""
                End If
            End Function

            Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, language As String) As Object Implements IValueConverter.ConvertBack
                Throw New NotImplementedException
            End Function
        End Class

        Public Enum CharacterCase
            Lower = 0
            Upper = 1
            Normal = 2
        End Enum

The problem is that the case conversion is not working, could you please correct me or advice where I have went wrong.

EDIT: Modified Xaml code with converter defined as static resource:

xmlns:cc="using:MCPhone81.MCCommonCodes.Converters"
 <Page.Resources>
        <cc:CaseConverter x:Key="lowercaseconverter" SelectedCase="Lower"/>
    </Page.Resources>
    <TextBox Text="{Binding Obj.UserName,Mode=TwoWay,Converter={StaticResource lowercaseconverter}}" />

Code for the property:

Property UserName As String
                Get
                    Return _UserNameValue
                End Get
                Set(value As String)
                    value = value.ToLower
                    If Not _UserNameValue.ToLower = value Then
                        _UserNameValue = value
                        NotifyPropertyChanged("UserName")
                    End If
                End Set
            End Property

However text is not changing case.

1

There are 1 best solutions below

1
On

You can't bind the Converter property of a Binding as you did in

Text="{Binding ..., Converter={Binding UpperCaseConverter}}"

You should instead instantiate the converter as resource

<Page.Resources>
    <local:CaseConverter x:Key="UpperCaseConverter"/>
</Page.Resources>

and reference it by a StaticResource expression

Text="{Binding ..., Converter={StaticResource UpperCaseConverter}}"