Error Object of type 'System.Windows.Data.Binding' cannot be converted to type 'System.String'

Ajeje Brazorf 106 Reputation points
2021-11-09T08:09:36.093+00:00

Hello,
I've this usercontrol here: showthread.php
that have the property SelectedEndianess

and in my app the the property:

  Private Const m_Defaultendianess = 0
    Private _myendianess As String = m_Defaultendianess
    <DefaultValue(m_Defaultendianess)>
    Public Property myEndianess() As String
        Get
            Return _myendianess
        End Get
        Set(ByVal value As String)
            SetAndNotify(_myendianess, value) 
        End Set
    End Property

I'm trying to bind them so:

<local:myBitButton  x:Name="mybtnBitButton" Grid.Row="1" EndianessChanged="{s:Action mybtnBitButton_EndianessChanged}" SelectedEndianess="{Binding myEndianess , UpdateSourceTrigger=PropertyChanged}" ></local:myBitButton>

but I'm getting this error: XDG0062 Object of type 'System.Windows.Data.Binding' cannot be converted to type 'System.String'.

I've also another similar usercontrol with the same iusse..
I get also System.Windows.Markup.XamlParseException: 'You cannot set 'Binding' for the 'SelectedEndianess' property of type 'myBitButton'. You can only set 'Binding' for a DependencyProperty element of a DependencyObject element.'

Any advice?

Developer technologies | Windows Presentation Foundation
Developer technologies | VB
{count} vote

Accepted answer
  1. Peter Fleischer (former MVP) 19,341 Reputation points
    2021-11-09T21:11:20.553+00:00

    Hi Ajeje,
    for binding you must implement DependencyProperty like this:

      Public Shared ReadOnly myEndianessProperty As DependencyProperty =
                DependencyProperty.RegisterAttached("myEndianess", GetType(String),
                  GetType(Window023UC1), New PropertyMetadata(String.Empty, AddressOf OnmyEndianessPropertyChanged))
    
      Private Shared Sub OnmyEndianessPropertyChanged(d As DependencyObject, e As DependencyPropertyChangedEventArgs)
        Dim uc = TryCast(d, Window023UC1)
        If uc Is Nothing OrElse e.NewValue Is Nothing Then Exit Sub
        uc.myEndianess = e.NewValue.ToString
      End Sub
    
      Public Shared Function GetFarbe(obj As DependencyObject) As Brush
        Return CType(obj.GetValue(myEndianessProperty), Brush)
      End Function
    
      Public Shared Sub SetFarbe(obj As DependencyObject, value As Brush)
        obj.SetValue(myEndianessProperty, value)
      End Sub
    
      Private Const m_Defaultendianess = "0"
      Private _myendianess As String = m_Defaultendianess
      <DefaultValue(m_Defaultendianess)>
      Public Property myEndianess() As String
        Get
          Return _myendianess
        End Get
        Set(ByVal value As String)
          SetAndNotify(_myendianess, value)
        End Set
      End Property
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.