dotMorten / WinUIEx

WinUI Extensions
https://dotmorten.github.io/WinUIEx
MIT License
602 stars 38 forks source link

DataContext not found. #11

Closed logue closed 2 years ago

logue commented 2 years ago

I'm trying to develop with this library in MVVM architecture using Prism or CommunityToolkit.Mvvm, but in MainWindow.xaml.cs I get an error that DataContext is not an attachable property.

I'm not very familiar with MVVM, but if I want to make the following code into ViewModel, how should I set it on the View side?

namespace WinUIExSample.ViewModels
{
    using CommunityToolkit.Mvvm.ComponentModel;
    using CommunityToolkit.Mvvm.Input;
    using Microsoft.UI.Xaml;

    public class MainWindowViewModel : ObservableObject
    {
        public IRelayCommand ExitCommand { get; }

        public MainWindowViewModel()
        {
            ExitCommand = new RelayCommand(ExecuteExitCommand);
        }

        private void ExecuteExitCommand()
        {
            Application.Current.Exit();
        }
    }
}
dotMorten commented 2 years ago

A WinUI window isn't a `` DependencyObject so it doesn't have a data context. You'll need to set the data context on the content object instead.