Search code examples
c#wpfxamldependency-propertiesdependencyobject

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


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?


Solution

  • 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.