0

In my mainwindow I have got an UserControl, which ViewModel has got the Dependency Property "Message", I'm trying to bind the Dependency Property to an Property of the ViewModel of the Main Window, but actually it isn't Working, is there any soulution or is it genarally impossible?

Content of the Main Window:

    <local:MessageLayer>
        <local:MessageLayer.DataContext>
            <local:MessageBoxViewModel Message="{Binding RelativeSource={RelativeSource AncestorType=Window, Mode=FindAncestor}, Path=DataContext.Message}"/>
        </local:MessageLayer.DataContext>
    </local:MessageLayer>
Cedric Krause
  • 361
  • 1
  • 6
  • 15
  • Simply use Path=Message instead of Path=DataContext.Message – Sivasubramanian Jul 10 '14 at 08:24
  • That doesn't solves the Problem, for both ways I got these output: System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Window', AncestorLevel='1''. BindingExpression:Path=Message; DataItem=null; target element is 'MessageBoxViewModel' (HashCode=63995229); target property is 'Message' (type 'String') – Cedric Krause Jul 10 '14 at 08:27
  • 1
    @Sivasubramanian, no, because he also specified a RelativeSource – Thomas Levesque Jul 10 '14 at 08:27
  • btw, using a DependencyObject as a ViewModel is quite unusual, and with good reasons. Check out this question: http://stackoverflow.com/questions/291518/inotifypropertychanged-vs-dependencyproperty-in-viewmodel – Thomas Levesque Jul 10 '14 at 08:28
  • @ThomasLevesque alright, is there an other way to bind the Message of the MessageBoxViewModel to the Message Property of the MainWindow? I think the Message Property of the M;essageBoxViewModel has to be an DependencyProperty – Cedric Krause Jul 10 '14 at 08:32
  • 1
    @CedricKrause, no, that's not necessary; you just need to implement INotifyPropertyChanged. But anyway, that's probably not the cause of your problem. Have you tried to use ElementName instead of RelativeSource? – Thomas Levesque Jul 10 '14 at 08:38
  • Yes I tried this before using the RelativeSource. How could I solve this using an PropertyChanged, how should I set the Message of the Message layer – Cedric Krause Jul 10 '14 at 08:41
  • I simply could use the DataContext of the MainWindow for the MessageBoxLayer – Cedric Krause Jul 10 '14 at 08:46

1 Answers1

2

View models should not have DependencyPropertys in them and should certainly not extend the DependencyObject class, because these are UI related classes. I'm sure that you, along with many others have been confused by Microsoft's terribly worded error below:

A 'Binding' can only be set on a DependencyProperty of a DependencyObject.

This is really only referring to the UI element side of Bindings and not the data element side. For data binding data objects, we implement the INotifyPropertyChanged interface instead, which provides similar property change notification functionality to DependencyPropertys.

So, had you set the Window.DataContext to an instance of a view model that implemented INotifyPropertyChanged, with a property declared in it named Message, then your code would have worked just fine:

<local:MessageLayer>
    <local:MessageLayer.DataContext>
        <local:MessageBoxViewModel Message="{Binding RelativeSource={RelativeSource 
            AncestorType=Window, Mode=FindAncestor}, Path=DataContext.Message}"/>
    </local:MessageLayer.DataContext>
</local:MessageLayer>
Sheridan
  • 68,826
  • 24
  • 143
  • 183