I bind my wpf window to app layer class (WindowVM.cs) using DataContext in Window.xaml.cs constructor (DataContext = WindowVM). But, one control (btnAdd) I want to bind to Window.xaml.cs property. So in Window.xaml.cs constructor I add this.btnAdd.DataContext. This is Window.xaml.cs constructor and property to which I want bind Button btnAdd:
public Window() { InitializeComponent(); DataContext = WindowVM; this.btnAdd.DataContext = this; } public RelayCommand Add { get { return _add == null ? _add= new RelayCommand(AddPP, CanAddPP) : _add; } set { OnPropertyChanged("Add"); } } Xaml looks like this (class PP is WindowVM property):
<TextBox Name="txtName" Text="{Binding PP.Name, ValidatesOnDataErrors=true, UpdateSourceTrigger=PropertyChanged}" /> <TextBox Name="txtSurname" Text="{Binding PP.Surname, ValidatesOnDataErrors=true, UpdateSourceTrigger=PropertyChanged}" /> <Button Command="{Binding Add}" Content="Add" ... /> And - everything works, but console output this:
BindingExpression path error: 'Add' property not found on 'object' ''WindowVM'... In next calls there isn't any console output error for property Add.
Now I am a little bit confused because of this error. Is this error because of first DataContext (to WindowVM), because there isn't property Add, but with line this.btnAdd.DataContext property Add is found and it's the reason that it works?