Detect in XAML broken bindings already at compile

2019-01-15 02:18发布

问题:

It is a simple and frequent, I think, scenario - you bind in XAML a dependent property to property in the viewmodel and later rename the property in viewmodel and forget to rename it in XAML. So is the binding broken.

Is there a way to detect such a broken bindings already at compile time?

回答1:

you could add the VM-class to your Binding line this:

<DataGrid ItemsSource="{Binding Path=(viewModels:MyViewModel.MyItemsSource)}" />

with:

namespace ViewModels{
    public class MyViewModel{
        public ICollectionView MyItemsSource {get; set;}
    }
}

This shows you BindingErrors at DesignTime