英文:
Binding visibility.hidden in Xaml
问题
我有一个将布尔转换为可见性的转换器。
Imports System.Globalization
Public Class ValueConverters
Implements IValueConverter
Public Function Convert(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object
Return Visibility.Hidden
End Function
Public Function ConvertBack(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object
Throw New NotImplementedException()
End Function
Private Function IValueConverter_Convert(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.Convert
Throw New NotImplementedException()
End Function
Private Function IValueConverter_ConvertBack(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.ConvertBack
Throw New NotImplementedException()
End Function
End Class
我在Xaml中添加了资源:
xmlns:local="clr-namespace:AppKD_2017" d:DataContext="{d:DesignInstance Type=local:A_CreationAssemblage}"
mc:Ignorable="d"
Title="Données d'entrée grille de perçage" Height="400" Width="700"
MaxHeight="460" MaxWidth="700"
ResizeMode="NoResize" WindowStartupLocation="CenterScreen"
>
<Window.Resources>
<local:ValueConverters x:Key="BoolToVis" />
<!--<!--<BooleanToVisibilityConverter x:Key="BoolToVis" />-->-->
</Window.Resources>
我收到了以下错误信息:
"ValueConverters" 在 "clr-namespace:AppKD_2017" 命名空间中不存在。
英文:
I have a Boolean to Visibility converter
Imports System.Globalization
Public Class ValueConverters
Implements IValueConverter
Public Function Convert(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object
Return Visibility.Hidden
End Function
Public Function ConvertBack(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object
Throw New NotImplementedException()
End Function
Private Function IValueConverter_Convert(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.Convert
Throw New NotImplementedException()
End Function
Private Function IValueConverter_ConvertBack(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.ConvertBack
Throw New NotImplementedException()
End Function
End Class
I added the resources in my Xaml
xmlns:local="clr-namespace:AppKD_2017" d:DataContext="{d:DesignInstance Type=local:A_CreationAssemblage}"
mc:Ignorable="d"
Title="Données d'entrée grille de perçage" Height="400" Width="700"
MaxHeight="460" MaxWidth="700"
ResizeMode="NoResize" WindowStartupLocation="CenterScreen"
>
<Window.Resources>
<local:ValueConverters x:Key="BoolToVis" />
<!--<BooleanToVisibilityConverter x:Key="BoolToVis" />-->
</Window.Resources>
I have this error message
The name "ValueConverters" does not exist in the "clr-namespace:AppKD_2017" namespace
答案1
得分: 1
您的转换器存在一些问题。实现IValueConverter接口的两个方法都抛出了NotImplementedException。Convert
方法中的代码永远不会通过接口被调用。此外,无论接收到哪个布尔值,它总是返回Hidden。考虑以下修改:
Imports System.Globalization
Public Class ValueConverters
Implements IValueConverter
Public Function Convert(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.Convert
If value.GetType = GetType(Boolean) Then
Select Case CType(value, Boolean)
Case True
Return Visibility.Visible
Case False
Return Visibility.Hidden
End Select
End If
End Function
Public Function ConvertBack(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
Throw New NotImplementedException()
End Function
End Class
最后,在转换器被XAML编辑器识别之前,您可能需要先构建您的项目。
英文:
Your converter has a few problems. The two methods implementing the IValueConverter interface both throw a NotImplementedException. Convert
, where you have written some code, will never get called through the interface. Furthermore, it always returns Hidden no matter which Boolean value it receives. Consider these changes:
Imports System.Globalization
Public Class ValueConverters
Implements IValueConverter
Public Function Convert(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.Convert
If value.GetType = GetType(Boolean) Then
Select Case CType(value, Boolean)
Case True
Return Visibility.Visible
Case False
Return Visibility.Hidden
End Select
End If
End Function
Public Function ConvertBack(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
Throw New NotImplementedException()
End Function
End Class
Finally, you probably have to Build your project before the converter is recognized by the XAML editor.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论