Simple question, but I dons see solution. Or may be dont understand how Bind method works. The goal is two way binding between ViewModel and DataContext properties.
public MainWindow()
{
InitializeComponent();
this.Bind(this, v => v.DataContext, v => v.ViewModel);
}
public static readonly DependencyProperty ViewModelProperty = DependencyProperty.Register(
"ViewModel", typeof (string), typeof (MainWindow));
public string ViewModel
{
get { return (string) GetValue(ViewModelProperty); }
set { SetValue(ViewModelProperty, value); }
}
when I set ViewModel property, I get InvalidCastException "System.String" to "WpfApplication1.MainWindow".
But xaml binding works perfectly.
<MainWindow
DataContext="{Binding RelativeSource={RelativeSource Self}, Path=ViewModel, Mode=TwoWay}" ...
full xaml.cs/xaml code is here http://pastebin.com/iCKeNS7R
Where I wrong ?
update: this code:
this.WhenAnyValue(v => v.ViewModel).BindTo(this, v => v.DataContext);
this.WhenAnyValue(v => v.DataContext).BindTo(this, v => v.ViewModel);
also works as expected
update 2 Question: Does this.Bind(viewModelParam, ...) ignore viewModelParam argument ??
example^ http://pastebin.com/e2aPaGNc
I bind to _otherViewModel, but when type text into textBox, ViewModel.StrProp changed, not _otherViewModel.
Does anybody know, how this.Bind work ??