0
votes

I have the following enumeration

Enum NodeStatusTypes
    Undefined
    Grant
    Deny
End Enum

and I'm trying to bind a class to a listbox so that each instance of the class gets a name and permission entry in the listbox bound to a textbox and 3-state checkbox. The code below works partially, in that if I add a class object whose permission property is Grant then the checkbox will be checked. However I also need the checkbox to be unchecked for objects whose permission is Deny and for the checkbox to be in the "null" state (eg IsChecked="null") when permission is Undefined. I'm almost certain the issue is with the ConverterParameter, but I can't figure out how to handle this.

<ListBox>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <StackPanel.Resources>
                    <l:EnumToTriStateConverter x:Key="TriConverter" />
                </StackPanel.Resources>
                <CheckBox IsThreeState="True" IsChecked="{Binding Path=Permission, Converter={StaticResource TriConverter}, ConverterParameter={x:Static l:NodeStatusTypes.Grant}}"  />
                <TextBlock Text="{Binding Path=Name}" />
                <TextBlock Text="{Binding Path=Permission}" />
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

Here is the converter class per request:

Public Class EnumToTriStateConverter
    Implements IValueConverter

    Public Function Convert(value As Object, targetType As System.Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert
        Return value.Equals(parameter)
    End Function

    Public Function ConvertBack(value As Object, targetType As System.Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack
        Dim retVal As NodeStatusTypes = Nothing
        Select Case value
            Case Nothing
                retVal = NodeStatusTypes.Undefined
            Case True
                retVal = NodeStatusTypes.Grant
            Case False
                retVal = NodeStatusTypes.Deny
        End Select
        Return retVal
    End Function

End Class
1
Could you share the converters code too? - BigL

1 Answers

1
votes

The correct implementation would look something like this:

Public Function Convert(value As Object, targetType As System.Type,
                        parameter As Object,
                        culture As System.Globalization.CultureInfo) As Object
Implements System.Windows.Data.IValueConverter.Convert

    Dim retVal As Object = Nothing;
    Select Case value
        Case NodeStatusTypes.Undefined
            retVal = Nothing
        Case NodeStatusTypes.Grant
            retVal = True
        Case NodeStatusTypes.Deny
            retVal = False
    End Select

    Return retVal

End Function

The converter parameter doesn't seem to make much sense, you can remove it from your binding.