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

时间:2016-04-14 12:13:37

标签: c# xaml mvvm binding datacontext

在这个例子中,我尝试开发一个简单的工具来管理客户的软件许可证。它在C#WPF中实现,中央设计模式基于MVVM范例(使用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?!

2 个答案:

答案 0 :(得分:0)

您在DataGrid上输入了错误的ItemsSource:

DataGrid ItemsScource =&#34; {Binding Licenses}&#34; SelectedItem =&#34; {Binding SelectedLicense}&#34;

答案 1 :(得分:0)

我的旧ViewModel属性(不工作)

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

我的新ViewModel属性(正在运行)

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