0

I am trying to update a previous built form to use styles to help clean up the code and better prepare for a switch to MVVM. We have logic that needs to mark the column read only until a check box is checked.

I have tried binding to the checkbox itself, but received a binding error saying it couldn't find the check box.

IsReadOnly="{Binding ElementName=ckbPerUnitType, Path=IsChecked, Converter={StaticResource BoolInv}, ConverterParameter=True}"

I have tried binding to the parent user controls data context, which the check box is bound to. I received an error that the source for the binding reference can not be found.

 IsReadOnly="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType=UserControl}, Path=DataContext.RentAdj.blnIsDifferentForType, Converter={StaticResource BoolInv}, ConverterParameter=True}"

I'm not sure what I am missing.

Here is the code for the Converter that is Converter used in both

<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
Bryan
  • 357
  • 4
  • 16

1 Answers1

0

In researching a different issue, I came across this answer and it actaully solved my issue. Here is the working solution

<CheckBox x:Name="ckbPerUnitType" />
....
 IsReadOnly="{Binding Source={x:Reference ckbPerUnitType},  Path=DataContext.RentAdj.blnIsDifferentForType, Converter={StaticResource BoolInv}, ConverterParameter=True}"
Community
  • 1
  • 1
Bryan
  • 357
  • 4
  • 16