简体   繁体   English

WPF数据绑定转换器.NET

[英]WPF Databinding Converter .NET

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". “sFileLocation”是我的类“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. 我不明白的事情是,如果我只是将属性绑定到Button.Content,绑定工作正常。 So the property is updating correctly when it gets changed, and the buttons content changes accordingly. 因此,属性在更改时正确更新,按钮内容也会相应更改。

Without seeing the rest of your code it sounds like your ViewModel or wherever you're binding to is not implementing INotifyPropertyChanged. 在没有看到代码的其余部分的情况下,它听起来像您的ViewModel或您绑定到的任何地方都没有实现INotifyPropertyChanged。

Also, is there any reason why you're binding in the code-behind and not in the XAML? 另外,你有什么理由在代码隐藏中绑定而不是在XAML中绑定吗? After defining your Visibility Converter as a resource: 将Visibility Converter定义为资源后:

<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}}" />

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... 你的字符串需要实现的类INotifyPropertyChanged: Implements INotifyPropertyChanged然后setter需要通知世界它已经改变了...

see MSDN for more info , but here is a code snippet from their example: 有关详细信息请参阅MSDN ,但以下是其示例中的代码段:

   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

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM