I'm trying to update a property of an element in the XAML of a view:
this.WhenAnyValue(x => x.ViewModel.IsEnabled).BindTo(this, x => x.MyButton.IsEnabled);
This works as expected, however, it generates a warning at runtime:
POCOObservableForProperty: rx_bindto_test.MainWindow is a POCO type and won't send change notifications, WhenAny will only return a single value!
I can get rid of the warning by changing the expression to:
this.WhenAnyValue(x => x.ViewModel.IsEnabled).Subscribe(b => MyButton.IsEnabled = b);
but I'm still wondering why it doesn't work properly with BindTo().
Edit: it appears even the regular Bind
and OneWayBind
generate this warning.
- What am I doing wrong here?
- And is it really necessary to define
ViewModel
as a dependency property on the View to be able to observe it? (when I declare it as a regular property on the View, ReactiveUI generates the same POCO warning) I can't simply make it inherit from ReactiveObject because C# doesn't support multiple inheritance.
MainWindow.xaml.cs
public partial class MainWindow : Window, IViewFor<MyViewModel>, IEnableLogger {
public static readonly DependencyProperty ViewModelProperty = DependencyProperty.Register("ViewModel",
typeof(MyViewModel), typeof(MainWindow));
public MyViewModel ViewModel {
get { return (MyViewModel)GetValue(ViewModelProperty); }
set { SetValue(ViewModelProperty, value); }
}
object IViewFor.ViewModel {
get { return ViewModel; }
set { ViewModel = (MyViewModel)value; }
}
public MainWindow() {
InitializeComponent();
this.WhenAnyValue(x => x.ViewModel).BindTo(this, x => x.DataContext);
this.WhenAnyValue(x => x.ViewModel.IsEnabled).BindTo(this, x => x.MyButton.IsEnabled);
ViewModel = new MyViewModel();
ViewModel.IsEnabled = true;
}
}
MainWindow.xaml
<Window x:Class="rx_bindto_test.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button x:Name="MyButton">My Button</Button>
</Grid>
</Window>
MyViewModel.cs
public class MyViewModel : ReactiveObject, IEnableLogger {
private bool isEnabled;
public bool IsEnabled {
get { return isEnabled; }
set { this.RaiseAndSetIfChanged(ref isEnabled, value); }
}
}