2011-08-09 62 views
0

我有一個按鈕,我基本上想顯示或隱藏它的基礎上,如果某個字符串有值或不。我在代碼中創建按鈕,所以我試圖用一個轉換器使用數據綁定,但我似乎無法在值更改後獲得綁定上的轉換器。我不知道我是否正確地跟着這一點......這是我創建按鈕和綁定和轉換器。 「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。因此,屬性在更改時會正確更新,並且按鈕內容會相應更改。

+0

不知道發生了什麼錯誤的轉換器被調用,但一旦這樣排序,你需要從[能見度枚舉]返回一個值(http://msdn.microsoft.com/en-us/library /system.windows.visibility.aspx),如果你綁定了控件的Visibility屬性,而不是字符串。 –

回答

2

沒有看到你的代碼的其餘部分,它聽起來像你的ViewModel或無論你綁定到哪裏沒有實現INotifyPropertyChanged。

另外,是否有任何理由爲什麼你綁定在代碼隱藏,而不是在XAML?定義你的能見度轉換爲資源後:

<ViewButtonConverter x:Key="VisibilityConverter" /> 

你可以在使用下列內容:

<Button x:Name="button" Content="Click Me" Visibility="{Binding Path=sFileLocation, Converter={StaticResource VisibilityConverter}}" /> 
+0

我想補充一點,你的轉換器的結果應該是'Visibility',而不是一個字符串。例如 '結果= Visibility.Visible' –

+0

尼斯趕上蒂姆。我完全錯過了。 – Josh

+0

我在代碼中綁定,因爲我基於我從數據庫中獲得的所有詳細信息創建了所有代碼中的按鈕和UI。 – bflosabre91

0

您的字符串生活需要執行INotifyPropertyChanged類:一則制定者需要通知Implements INotifyPropertyChanged它已經改變了世界......

see MSDN for more info,但這裏是他們的示例代碼片段:

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 
0

我遇到的問題是設置轉換器參數。一旦我擺脫了這一點,它按預期工作。我非常感謝你的幫助,繼承人對我有用。

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)