我的 MVVM 模式不工作......为什么?

My MVVM pattern is not working... Why?

在这个例子中,我尝试开发一个简单的工具来为客户管理软件许可证。它是在 C# WPF 中实现的,中心设计模式基于 MVVM 范例(表示层和 logic/data 层与 INotifyPropertyChanged、ICommand 和其他东西的分离)。

为什么无法动态绑定 XAML 中的属性? 请查看末尾的附加链接以获取更多信息。

感谢您的帮助!

菲利克斯

我的相关观点(XAML):

<DataGrid ItemsScource="{Binding Licenses}" SelectedItem="{Binding SelectedLicense}">
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding CustomerConverted}" Header="Customer"/>
                <DataGridTextColumn Binding="{Binding LicenseNoConverted}" Header="License no."/>
                <DataGridTextColumn Binding="{Binding LicenseKey}" Header="License key"/>
                <DataGridTextColumn Binding="{Binding LastActivation}" Header="Last activated ver."/>
                <DataGridTextColumn Binding="{Binding TypeConverted}"/>
            </DataGrid.Columns>
        </DataGrid> 

代码隐藏

public partial class WMain : Window {
    public WMain() { 
        InitializeComponent();

        //construct ViewModel (works fine, all properties are consistent)
        WMainCtrl ctrl = new WMainCtrl(); 

        //set data context in code behind (XAML-bindings are not working)
        this.DataContext = ctrl;

        //that works instead... why?
        //c_dgLicenses.ItemsSource = ctrl.Licenses;        
    }
}

视图模型:

public class WMainCtrl : BaseCtrl {

    public WMainCtrl() {
        try {
            //init data
            License.selectAndConvertData();
        } catch (Exception exc) {
            throw exc;
        }
    }

    //this works, the collection is definitely filled 
    internal ObservableCollection<License> Licenses { get { return License.Obscol; } }

    private License _selectedLicense;
    public License SelectedLicense {
        get {
            if (_selectedLicense == null) {
                if (Licenses.Count == 0) {
                    return null;
                }
                return Licenses[0];
            } else
            return _selectedLicense;
        }
        set {
            if (_selectedLicense != value) {
                _selectedLicense = value;
                try {
                    Console.WriteLine(SelectedLicense.LicenseKey);
                } catch (Exception) {}
                OnPropertyChanged();
            }
        }
    }
}

ViewModelBase 的相关部分:

    protected void OnPropertyChanged([CallerMemberName] string propertyName = null) {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;

以下是一些详细信息:

Architecture (rough draft, no UML rules)

Screenshot: DataContext is set properly?!

您在 DataGrid 上输入错误的 ItemsSource:

DataGrid ItemsScource="{Binding Licenses}" SelectedItem="{Binding SelectedLicense}"

我的旧 ViewModel 属性(不工作)

internal ObservableCollection<License> Licenses { get { return License.Obscol; } }

我的新 ViewModel 属性(工作中)

public ObservableCollection<License> Licenses { get { return License.Obscol; } }