locked
Databinding works in Visual Studio 2013 but not actual app

    Question

  • I have an app in which I only want to display the next TextBox only when the first one has something entered into it. I am using an IValueConverter to do this. Here is my IValueConverter and the relevant XAML:
    Public Class HideValueVisibilityConverter : Implements IValueConverter
    	Public Function Convert(value As Object, targetType As Type, parameter As Object, language As String) As Object Implements IValueConverter.Convert
    		If parameter Is Nothing Then
    			If value.ToString() = "0" Then Return Visibility.Collapsed Else Return Visibility.Visible
    		Else
    			If value.ToString() = parameter.ToString() Then Return Visibility.Collapsed Else Return Visibility.Visible
    		End If
    	End Function
    	Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, language As String) As Object Implements IValueConverter.ConvertBack
    		Return value
    	End Function
    End Class

    <TextBox Grid.Column="0" Grid.Row="2" Style="{StaticResource PlayerNameTextBoxStyle}" PlaceholderText="Player 1" TextChanged="Name_TextChanged"/>
    <TextBox Grid.Column="1" Grid.Row="2" Style="{StaticResource CardCountTextBoxStyle}" TextChanged="CardCount_TextChanged"/>
    <TextBox Grid.Column="0" Grid.Row="3" Style="{StaticResource PlayerNameTextBoxStyle}" PlaceholderText="Player 2" Visibility="{Binding Parent.Children[2].Text,RelativeSource={RelativeSource Mode=Self},Converter={StaticResource HideValueVisibility},ConverterParameter=''}" TextChanged="Name_TextChanged"/>
    <TextBox Grid.Column="1" Grid.Row="3" Style="{StaticResource CardCountTextBoxStyle}" Visibility="{Binding Parent.Children[2].Text,RelativeSource={RelativeSource Mode=Self},Converter={StaticResource HideValueVisibility},ConverterParameter=''}" TextChanged="CardCount_TextChanged"/>
    In Visual Studio 2013, the second set of TextBoxes are Visible/Collapsed like I want depending on the Text property of the first TextBox, but when I actually run the app it is displayed regardless of whether I have entered anything into the first TextBox. I would expect the Visibility of the second set of TextBoxes to change when I enter or remove text from the first TextBox. Am I doing something wrong? Why is Visual Studio 2013 showing it differently from the actual app? Any help would be appreciated. Thanks.

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Wednesday, June 4, 2014 3:39 AM

Answers

  • Hi,

    If you want to pass an empty string as a ConverterParameter, you should convert the null to string. Some codes below:

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
            <Grid.Resources>
                <local:HideValueVisibility x:Key="HideValueVisibility" />
            </Grid.Resources>
            <TextBox HorizontalAlignment="Left" Name="tb" Margin="478,124,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="258"/>
            <TextBox HorizontalAlignment="Left" Name="tb1" Margin="438,219,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="351" Visibility="{Binding Text,ElementName=tb,Converter={StaticResource HideValueVisibility},ConverterParameter=''}"/>
    
        </Grid>
    Public NotInheritable Partial Class MainPage
    	Inherits Page
    	Public Sub New()
    		Me.InitializeComponent()
    	End Sub
    	Public Shared Function NullToString(Value As Object) As String
    
    		' Value.ToString() allows for Value being DBNull, but will also convert int, double, etc.
    		Return If(Value Is Nothing, "", Value.ToString())
    
    		' If this is not what you want then this form may suit you better, handles 'Null' and DBNull otherwise tries a straight cast
    		' which will throw if Value isn't actually a string object.
    		'return Value == null || Value == DBNull.Value ? "" : (string)Value;
    
    
    	End Function
    End Class
    
    
    Public Class HideValueVisibility : Implements IValueConverter
        Public Function Convert(value As Object, targetType As Type, parameter As Object, language As String) As Object Implements IValueConverter.Convert
               If value.ToString() = MainPage.NullToString(parameter) Then
    	Return Visibility.Collapsed
               Else
    	Return Visibility.Visible
              End If
           
        End Function
        Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, language As String) As Object Implements IValueConverter.ConvertBack
            Return value
        End Function
    End Class

    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


    Thursday, June 5, 2014 7:08 AM

All replies

  • Hi, please try this minor change:

    <TextBox Grid.Column="0" Grid.Row="2" Style="{StaticResource PlayerNameTextBoxStyle}" PlaceholderText="Player 1" TextChanged="Name_TextChanged"/>
    <TextBox Name="tb" Grid.Column="1" Grid.Row="2" Style="{StaticResource CardCountTextBoxStyle}" TextChanged="CardCount_TextChanged" />
    <TextBox Visibility="{Binding Text,ElementName=tb,Converter={StaticResource HideValueVisibility},ConverterParameter=0}" Grid.Column="0" Grid.Row="3" Style="{StaticResource PlayerNameTextBoxStyle}" PlaceholderText="Player 2"  TextChanged="Name_TextChanged"/>
    <TextBox Visibility="{Binding Text,ElementName=tb,Converter={StaticResource HideValueVisibility},ConverterParameter=0}" Grid.Column="1" Grid.Row="3" Style="{StaticResource CardCountTextBoxStyle}"  TextChanged="CardCount_TextChanged"/>
    
    Public Class HideValueVisibilityConverter : Implements IValueConverter
        Public Function Convert(value As Object, targetType As Type, parameter As Object, language As String) As Object Implements IValueConverter.Convert
            If parameter Is Nothing Then
                If value.ToString() = "0" Then Return Visibility.Collapsed Else Return Visibility.Visible
            Else
                If value.Length = parameter Then Return Visibility.Collapsed Else Return Visibility.Visible
            End If
        End Function
        Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, language As String) As Object Implements IValueConverter.ConvertBack
            Return value
        End Function
    End Class


    Wednesday, June 4, 2014 10:01 AM
  • That does not work, because it sets the Visibility based on whether or not the TextBox has a value of 0; I want it to be based on whether or not it has a value of "" (an empty string). But I may have figured out why it is not doing what I want; the ConverterParameter is being passed Nothing rather than a String. But if I cannot use ConverterParameter='' to pass a String, how do I pass it? Thanks.

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Wednesday, June 4, 2014 4:21 PM
  • I was hoping I wouldn't need to resort to this so that I could reuse my existing IValueConverter, but I wrote a more specific IValueConverter for empty strings so that it doesn't need a parameter:
    Public Class HideEmptyVisibilityConverter : Implements IValueConverter
    	Public Function Convert(value As Object, targetType As Type, parameter As Object, language As String) As Object Implements IValueConverter.Convert
    		If parameter IsNot Nothing AndAlso parameter.ToString().ToUpper() = "NOT" Then : Return If(Not String.IsNullOrWhiteSpace(value.ToString()), Visibility.Collapsed, Visibility.Visible)
    		Else : Return If(String.IsNullOrWhiteSpace(value.ToString()), Visibility.Collapsed, Visibility.Visible)
    		End If
    	End Function
    	Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, language As String) As Object Implements IValueConverter.ConvertBack
    		Return value
    	End Function
    End Class
    This worked, so I guess I will need to use it for now. I would, however, still like to know how to pass an empty string as a ConverterParameter in XAML. Thanks.

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Wednesday, June 4, 2014 6:09 PM
  • Hi,

    If you want to pass an empty string as a ConverterParameter, you should convert the null to string. Some codes below:

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
            <Grid.Resources>
                <local:HideValueVisibility x:Key="HideValueVisibility" />
            </Grid.Resources>
            <TextBox HorizontalAlignment="Left" Name="tb" Margin="478,124,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="258"/>
            <TextBox HorizontalAlignment="Left" Name="tb1" Margin="438,219,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="351" Visibility="{Binding Text,ElementName=tb,Converter={StaticResource HideValueVisibility},ConverterParameter=''}"/>
    
        </Grid>
    Public NotInheritable Partial Class MainPage
    	Inherits Page
    	Public Sub New()
    		Me.InitializeComponent()
    	End Sub
    	Public Shared Function NullToString(Value As Object) As String
    
    		' Value.ToString() allows for Value being DBNull, but will also convert int, double, etc.
    		Return If(Value Is Nothing, "", Value.ToString())
    
    		' If this is not what you want then this form may suit you better, handles 'Null' and DBNull otherwise tries a straight cast
    		' which will throw if Value isn't actually a string object.
    		'return Value == null || Value == DBNull.Value ? "" : (string)Value;
    
    
    	End Function
    End Class
    
    
    Public Class HideValueVisibility : Implements IValueConverter
        Public Function Convert(value As Object, targetType As Type, parameter As Object, language As String) As Object Implements IValueConverter.Convert
               If value.ToString() = MainPage.NullToString(parameter) Then
    	Return Visibility.Collapsed
               Else
    	Return Visibility.Visible
              End If
           
        End Function
        Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, language As String) As Object Implements IValueConverter.ConvertBack
            Return value
        End Function
    End Class

    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


    Thursday, June 5, 2014 7:08 AM