我正在尝试更新以前构建的表单以使用样式来帮助清理代码并更好地准备切换到MVVM。我们有逻辑需要将列标记为只读,直到选中复选框。
我已尝试绑定到复选框本身,但收到绑定错误,说它无法找到复选框。
IsReadOnly="{Binding ElementName=ckbPerUnitType, Path=IsChecked, Converter={StaticResource BoolInv}, ConverterParameter=True}"
我已尝试绑定到父用户控件数据上下文,该复选框已绑定到该上下文。我收到一个错误,指出无法找到绑定引用的源。
IsReadOnly="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType=UserControl}, Path=DataContext.RentAdj.blnIsDifferentForType, Converter={StaticResource BoolInv}, ConverterParameter=True}"
我不确定我错过了什么。
以下是
中使用的Converter转换器的代码<ValueConversion(GetType(Boolean), GetType(Boolean))> _
Public Class BooleanInverseConverter
Implements IValueConverter
''' <summary>
''' Converter to Compare the Inverse of a Boolean
''' </summary>
''' <param name="value">Boolean you wish to compare</param>
''' <param name="targetType"></param>
''' <param name="parameter">Boolean to determine if the value needs to be inverted</param>
''' <param name="culture"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Function Convert(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert
Dim v As Boolean = DirectCast(value, Boolean)
If CBool(parameter) = True Then v = Not v
Return v
End Function
Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
Throw New Exception("Not Implemented")
End Function
End Class
答案 0 :(得分:0)
在研究一个不同的问题时,我遇到了this的答案,它实际上解决了我的问题。这是工作解决方案
<CheckBox x:Name="ckbPerUnitType" />
....
IsReadOnly="{Binding Source={x:Reference ckbPerUnitType}, Path=DataContext.RentAdj.blnIsDifferentForType, Converter={StaticResource BoolInv}, ConverterParameter=True}"