Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 8th, 2012  |  syntax: None  |  size: 2.17 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Binding an enumeration value to a 3-state checkbox
  2. Enum NodeStatusTypes
  3.     Undefined
  4.     Grant
  5.     Deny
  6. End Enum
  7.        
  8. <ListBox>
  9.     <ListBox.ItemTemplate>
  10.         <DataTemplate>
  11.             <StackPanel Orientation="Horizontal">
  12.                 <StackPanel.Resources>
  13.                     <l:EnumToTriStateConverter x:Key="TriConverter" />
  14.                 </StackPanel.Resources>
  15.                 <CheckBox IsThreeState="True" IsChecked="{Binding Path=Permission, Converter={StaticResource TriConverter}, ConverterParameter={x:Static l:NodeStatusTypes.Grant}}"  />
  16.                 <TextBlock Text="{Binding Path=Name}" />
  17.                 <TextBlock Text="{Binding Path=Permission}" />
  18.             </StackPanel>
  19.         </DataTemplate>
  20.     </ListBox.ItemTemplate>
  21. </ListBox>
  22.        
  23. Public Class EnumToTriStateConverter
  24.     Implements IValueConverter
  25.  
  26.     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
  27.         Return value.Equals(parameter)
  28.     End Function
  29.  
  30.     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
  31.         Dim retVal As NodeStatusTypes = Nothing
  32.         Select Case value
  33.             Case Nothing
  34.                 retVal = NodeStatusTypes.Undefined
  35.             Case True
  36.                 retVal = NodeStatusTypes.Grant
  37.             Case False
  38.                 retVal = NodeStatusTypes.Deny
  39.         End Select
  40.         Return retVal
  41.     End Function
  42.  
  43. End Class
  44.        
  45. Public Function Convert(value As Object, targetType As System.Type,
  46.                         parameter As Object,
  47.                         culture As System.Globalization.CultureInfo) As Object
  48. Implements System.Windows.Data.IValueConverter.Convert
  49.  
  50.     Dim retVal As Object = Nothing;
  51.     Select Case value
  52.         Case NodeStatusTypes.Undefined
  53.             retVal = Nothing
  54.         Case NodeStatusTypes.Grant
  55.             retVal = True
  56.         Case NodeStatusTypes.Deny
  57.             retVal = False
  58.     End Select
  59.  
  60.     Return retVal
  61.  
  62. End Function