我有一個按鈕,我基本上想顯示或隱藏它的基礎上,如果某個字符串有值或不。我在代碼中創建按鈕,所以我試圖用一個轉換器使用數據綁定,但我似乎無法在值更改後獲得綁定上的轉換器。我不知道我是否正確地跟着這一點......這是我創建按鈕和綁定和轉換器。 「sFileLocation」是我的課程「QuestionsFile」中的一個字符串。這適用於初始化,但只是當字符串的值發生變化時,該綁定沒有看到更改,並且不運行轉換器以及所有這些對我...感謝您的任何幫助。WPF Databinding Converter .NET
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
我不明白的事情是,綁定工作正常,如果我只是將屬性綁定到Button.Content。因此,屬性在更改時會正確更新,並且按鈕內容會相應更改。
不知道發生了什麼錯誤的轉換器被調用,但一旦這樣排序,你需要從[能見度枚舉]返回一個值(http://msdn.microsoft.com/en-us/library /system.windows.visibility.aspx),如果你綁定了控件的Visibility屬性,而不是字符串。 –