Reputation: 1549
I have a button that i basically want to either show or hide it based on if a certain string has a value or not. I create the buttons in code so i was trying to used databindings with a converter but i can't seem to get the converter on the binding after the value changes. I'm not sure if I'm going after this correctly or not... Here is what i have for creating the button and the binding and the converter. "sFileLocation" is a string inside my class "QuestionsFile". This works for initialization but its just when the value of the string changes, this binding doesn't see the change and doesn't run the converter and all that for me... thanks for any help.
Dim btn2 As New Button
Dim b2 As New Binding("sFileLocation")
b2.Mode = BindingMode.TwoWay
b2.Source = DirectCast(q, QuestionListClass.QuestionsFile)
b2.Converter = New ViewButtonConverter
b2.ConverterParameter = b2.Source
btn2.SetBinding(Button.VisibilityProperty, b2)
<ValueConversion(GetType(String), GetType(Visibility))> _
Public Class ViewButtonConverter
Implements IValueConverter
Public Function Convert(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert
Dim result As Visibility = Visibility.Collapsed
If parameter IsNot Nothing Then
If parameter.GetType Is GetType(String) Then
If DirectCast(parameter, String) <> "" Then
result = Visibility.Visible
Else
result = Visibility.Collapsed
End If
End If
End If
Return result
End Function
Public Function ConvertBack(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack
Return Nothing
End Function
End Class
'this how my class is set up now, its enormous or else id post all of it..
Public Class QuestionListClass
Public Class QuestionList
Inherits ObservableCollection(Of QuestionType)
End Class
End Class
The thing I don't understand too is that the binding works fine if i just binding the property to the Button.Content. So the property is updating correctly when it gets changed, and the buttons content changes accordingly.
Upvotes: 0
Views: 575
Reputation: 1549
the problem i had was with setting the Converter Parameter. Once i got rid of that, it worked as expected. I appreciate all of your help, heres what worked for me.
Dim b2 As New Binding("sFileLocation")
b2.Mode = BindingMode.TwoWay
b2.Source = DirectCast(q, QuestionListClass.QuestionsFile)
b2.Converter = New ViewButtonConverter
btn2.SetBinding(Button.VisibilityProperty, b2)
Upvotes: 0
Reputation: 29216
the class where your string lives needs to implement INotifyPropertyChanged: Implements INotifyPropertyChanged
an then the setter needs to notify the world that it has changed...
see MSDN for more info, but here is a code snippet from their example:
Public Property CustomerName() As String
Get
Return Me.customerNameValue
End Get
Set(ByVal value As String)
If Not (value = customerNameValue) Then
Me.customerNameValue = value
NotifyPropertyChanged("CustomerName")
End If
End Set
End Property
Upvotes: 0
Reputation: 2975
Without seeing the rest of your code it sounds like your ViewModel or wherever you're binding to is not implementing INotifyPropertyChanged.
Also, is there any reason why you're binding in the code-behind and not in the XAML? After defining your Visibility Converter as a resource:
<ViewButtonConverter x:Key="VisibilityConverter" />
You could use it in the following:
<Button x:Name="button" Content="Click Me" Visibility="{Binding Path=sFileLocation, Converter={StaticResource VisibilityConverter}}" />
Upvotes: 2