0

I keep getting a "A 'Binding' can only be set on a DependencyProperty of a DependencyObject." error in my XAML file and running the application isn't working either.

I'm just going to post the code because I've been staring at this for hours, with no resolution...

MainWindow.xaml

<local:ToolBarButton ViewModel="{Binding SelectedScreen.BackButtonViewModel}"/>

The above line is what's giving me an error on my MainWindow.xaml.

The SelectedScreen property is defined in my MainWindowViewModel. Code below.

MainWindowViewModel.cs

public class MainWindowViewModel : DependencyObject, INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged = delegate { };

    private ObservableCollection<ScreenViewModelBase> _screens;
    public ObservableCollection<ScreenViewModelBase> Screens
    {
        get { return _screens; }
        set
        {
            if (value != _screens)
            {
                _screens = value;
                PropertyChanged(this, new PropertyChangedEventArgs(nameof(Screens)));
            }
        }
    }

    public static readonly DependencyProperty SelectedScreenProperty =
        DependencyProperty.Register(nameof(SelectedScreen), typeof(ScreenViewModelBase), typeof(MainWindowViewModel));


    public ScreenViewModelBase SelectedScreen
    {
        get { return (ScreenViewModelBase)GetValue(SelectedScreenProperty); }
        set { SetValue(SelectedScreenProperty, value); }
    }



    public MainWindowViewModel()
    {
        Screens.Add(new HomeScreenViewModel());
        SelectedScreen = Screens[0];

    }
}

As you can see, my view model used as my DataContext on my MainWindow class is, in fact, a dependency property. Yet, I can't get this to work properly and I keep getting the error that's telling me that this isn't a dependency object.

Furthermore, here is the code to the ViewModel property on the associated user control.

ToolBarButton.xaml.cs (A User Control)

public partial class ToolBarButton : UserControl//, INotifyPropertyChanged
{

    public static DependencyProperty viewModelProperty = DependencyProperty.Register(nameof(ViewModel), typeof(ToolBarButtonViewModel), typeof(ToolBarButton));

    public ToolBarButtonViewModel ViewModel
    {
        get
        {
            return (ToolBarButtonViewModel)GetValue(viewModelProperty);
        }
        set
        {
            SetValue(viewModelProperty, value);
        }
    }
}

What am I missing?

RLH
  • 15,230
  • 22
  • 98
  • 182
  • Thanks @ash. The problem is that `ViewModel` is, in fact, a dependency property on a UserControl, which inherits from DependencyObject. – RLH May 29 '17 at 17:42
  • i was about to answer and then i noticed everything looks fine. Try to create the files again - we might be missing something quite obvious/simple. Also look at the debug window and implement the DebugConverter on the binding to help you debug – Luis Filipe May 29 '17 at 20:43
  • I'm not sure if that's the culprit, but according to [MSDN](https://msdn.microsoft.com/en-us/library/ms750428(v=vs.110).aspx) "The name of the identifier field that you use to store the name and characteristics of the dependency property must be the _Name_ you chose for the dependency property as part of the _Register_ call, appended by the literal string _Property_". In your case the field holding the dependency property should be named `ViewModelProperty` and not `viewModelProperty` (upper case _V_ instead lower case _v_). That's the only thing I can see that is wrong in your code. – Grx70 May 30 '17 at 06:32
  • I guess the BackButtonViewModel property actually returns a ToolBarButtonViewModel. But are you sure that it is this XAML markup that actually causes the error? I doubt it. Please provide a full repo of your issue if you need any further help: https://stackoverflow.com/help/mcve – mm8 May 30 '17 at 10:30

1 Answers1

0

I'm not sure what this was about. I tried all of these steps but I gave it one last try, doing all of them together and this fixed the problem. If anyone else is at their wits-end about this issue after following all other advice, try this.

  1. Close your XAML files.
  2. Clean your Solution.
  3. Close Visual Studio.
  4. Restart your PC. Yes, this seems like overkill, but it was part of what worked, so I'm including it.
  5. Re-open your solution and rebuild.
  6. Re-open your XAML file and the problem should be fixed.
StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
RLH
  • 15,230
  • 22
  • 98
  • 182