【发布时间】:2012-08-15 04:39:21
【问题描述】:
ViewModel 类:
class MyViewModel : ViewModelBase, INotifyPropertyChanged
{
public string SomeText { get{...} set {...}}
public AnyNonMVVMControl MyControl { get {...} set {...}}
...
}
窗口 XAML:
<Grid>
...
<TextBlock Text="{Binding SomeText}" />
<??? Content="{Binding MyControl}" /> <!-- how to bind MyControl to the View? -->
...
</Grid>
分配模型
...
window.DataContext = new MyViewModel(...);
...
我有一个控件,它不是为 MVVM 设计的。渲染和数据是强耦合的(糟糕的设计解决方案)。我的视图模型中有这个控件,并且想将它绑定到我的窗口。但我不想直接在 XAML 中添加控件,因为此控件还包含业务逻辑和数据 -> 我需要在视图模型中访问它以执行操作。
那么,如何通过 Bindings 将控件“添加”到窗口中呢?
【问题讨论】:
-
从视图模型中公开控件不是 MVVM,顺便说一句。