XAML绑定到ViewModel中的控件

0xD*_*EEF 1 .net c# wpf binding mvvm

ViewModel类:

class MyViewModel : ViewModelBase, INotifyPropertyChanged
{
    public string SomeText { get{...} set {...}}
    public AnyNonMVVMControl MyControl { get {...} set {...}}
    ...
}
Run Code Online (Sandbox Code Playgroud)

窗口XAML:

<Grid>
...
<TextBlock Text="{Binding SomeText}" />
<??? Content="{Binding MyControl}" /> <!-- how to bind MyControl to the View? -->
...
</Grid>
Run Code Online (Sandbox Code Playgroud)

分配模型

...
window.DataContext = new MyViewModel(...);
...
Run Code Online (Sandbox Code Playgroud)

我有一个Control,它不是为MVVM设计的.渲染和数据是强耦合的(糟糕的设计解决方案).我在我的viewmodel中有这个控件,并希望将它绑定到我的窗口.但我不想直接在XAML中添加Control,因为此控件还包含业务逻辑和数据 - >我需要在viewmodel中访问它以执行操作.

那么,如何通过Bindings将控件"添加"到窗口?

max*_*max 5

使用ContentPresenterContentControl:

<ContentControl Content="{Binding MyControl}" />
Run Code Online (Sandbox Code Playgroud)