0

First of all, I'd like to say that I was looking for an answer for over 3 days now. Yes, I know that Binding can only be done to a DependencyProperty. In my project I use DependencyProperty in several places, from many different Libraries and in many different windows/UserControls. All works just fine. But in one specific class I just can't get rid of that error... What's Interesting, It's only VS complaining about this (not existing) problem, as the project compiles and works as expected (the only reason I investigate is that due to that error both the builder and the designer have problems with refreshing Interface). Here is the class (I found the example somewhere on the Internet):

Public Class BindingProxy
    Inherits Freezable

    Public Shared ReadOnly DataProperty As DependencyProperty = DependencyProperty.Register("Data", GetType(Object), GetType(BindingProxy), New UIPropertyMetadata(AddressOf MyPropertyChangedHandler))

    Public Property Data As Object
        Get
            Return GetValue(DataProperty)
        End Get
        Set(value As Object)
            SetValue(DataProperty, value)
        End Set
    End Property

    Protected Overrides Function CreateInstanceCore() As Freezable
        Return New BindingProxy
    End Function

    Public Shared Sub MyPropertyChangedHandler(ByVal target As DependencyObject, ByVal args As DependencyPropertyChangedEventArgs)
        CType(target, BindingProxy).Data = args.NewValue
    End Sub
End Class

And It's used like this (here tha "Data" property is underlined and the error is shown):

<helpers:BindingProxy x:Key="proxy" Data="{Binding}" />

The Purpose of this is to bind templated controls (Like Button in DataTemplate for ListViews ItemTemplate) to the main ViewModel:

<Button Command="{Binding Data.DoSomethingCommand, Source={StaticResource proxy}}" CommandParameter="{Binding ID}" />

The BindingProxy class resides in a different project (same solution). As I said, the program compiles and works as expected. It's just the UI refreshing during designing and debugging that drives me crazy that made me looking for an answer...

Thanks in Advance, Piotr Lemański.

  • As a note, the PropertyChangedCallback is redundant. You don't need to assign `CType(target, BindingProxy).Data = args.NewValue` when the `Data` property has already changed. – Clemens Jul 12 '18 at 07:57
  • And for the purpose of a BindingProxy: in order to bind to a main view model from inside an ItemTemplate, you'd usually write `Command="{Binding DataContext.DoSomethingCommand, RelativeSource={RelativeSource AncestorType=ListView}}"` – Clemens Jul 12 '18 at 08:09
  • Good point on the callback! However, considering the puropose, I use sugguseted approach very often. The proxy is needed in cases like where the object is not part of the of the visual or logical tree, like in case of DataGridTemplateColumn (here it's telerik:RadMenuItem inside a HierarchicalDataTemplate). I might have forgotten to mention that in my post. I got the proxy idea from [link](https://stackoverflow.com/questions/15494226/cannot-find-source-for-binding-with-reference-relativesource-findancestor) – Piotr Lemański Jul 12 '18 at 12:24

0 Answers0