翻译为中文:在Xaml中绑定visibility.hidden。

huangapple go评论62阅读模式
英文:

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=&quot;clr-namespace:AppKD_2017&quot; d:DataContext=&quot;{d:DesignInstance Type=local:A_CreationAssemblage}&quot;
    mc:Ignorable=&quot;d&quot;
    Title=&quot;Donn&#233;es d&#39;entr&#233;e grille de per&#231;age&quot; Height=&quot;400&quot; Width=&quot;700&quot;
    MaxHeight=&quot;460&quot; MaxWidth=&quot;700&quot;
    ResizeMode=&quot;NoResize&quot; WindowStartupLocation=&quot;CenterScreen&quot;
    &gt;
&lt;Window.Resources&gt;
    &lt;local:ValueConverters x:Key=&quot;BoolToVis&quot; /&gt;
    &lt;!--&lt;BooleanToVisibilityConverter x:Key=&quot;BoolToVis&quot; /&gt;--&gt;
&lt;/Window.Resources&gt;

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.

huangapple
  • 本文由 发表于 2023年5月22日 20:36:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76306273.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定