【发布时间】:2015-05-20 14:21:09
【问题描述】:
我正在尝试更新视图 XAML 中元素的属性:
this.WhenAnyValue(x => x.ViewModel.IsEnabled).BindTo(this, x => x.MyButton.IsEnabled);
这按预期工作,但是,它会在运行时生成警告:
POCOObservableForProperty:rx_bindto_test.MainWindow 是 POCO 类型,不会发送更改通知,WhenAny 只会返回一个值!
我可以通过将表达式更改为:
this.WhenAnyValue(x => x.ViewModel.IsEnabled).Subscribe(b => MyButton.IsEnabled = b);
但我仍然想知道为什么它不能与 BindTo() 一起正常工作。
编辑:即使是常规的 Bind 和 OneWayBind 也会生成此警告。
- 我在这里做错了什么?
- 真的有必要将
ViewModel定义为View 的依赖属性以便能够观察到它吗? (当我将它声明为 View 上的常规属性时,ReactiveUI 会生成相同的 POCO 警告)我不能简单地让它从 ReactiveObject 继承,因为 C# 不支持多重继承。
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); }
}
}
【问题讨论】:
标签: c# wpf mvvm system.reactive reactiveui