2012-01-11 55 views
0

我有以下列举绑定枚举值到3态复选框

Enum NodeStatusTypes 
    Undefined 
    Grant 
    Deny 
End Enum 

,我试图将类绑定到一个列表框,使类的每个实例获得 一个权限条目绑定到文本框和3状态复选框。下面的代码,如果我添加一个类的对象,其许可属性是格兰特那么复选框将被选中的作品部分,在那。但是我还需要复选框为(例如=器isChecked“空”)时许可未定义对象,它们的许可拒绝和使用该复选框,在“空”的状态不被选中。我几乎可以肯定的问题是ConverterParameter,但我无法弄清楚如何处理这个问题。

<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> 

这里是每个请求的转换器类:

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

你能共享代码转换器也? – BigL 2012-01-11 16:55:45

回答

1

正确的实施将是这个样子:

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 

转换参数似乎并没有太大的意义,你可以从你的绑定中删除它。

+0

完美运作。我也为ConvertBack做了改变,这也起作用。 – WhiskerBiscuit 2012-01-11 17:19:50

+0

@WhiskerBiscuit:'ConvertBack'需要做什么修改?我认为从一开始就是正确的。 – 2012-01-11 18:12:40