XAML绑定到ViewModel中的控件

时间:2012-08-19 15:30:14

标签: c# .net wpf mvvm binding

ViewModel类:

class MyViewModel : ViewModelBase, INotifyPropertyChanged
{
    public string SomeText { get{...} set {...}}
    public AnyNonMVVMControl MyControl { get {...} set {...}}
    ...
}

Window XAML:

<Grid>
...
<TextBlock Text="{Binding SomeText}" />
<??? Content="{Binding MyControl}" /> <!-- how to bind MyControl to the View? -->
...
</Grid>

分配模型

...
window.DataContext = new MyViewModel(...);
...

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

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

1 个答案:

答案 0 :(得分:5)

使用ContentPresenterContentControl

<ContentControl Content="{Binding MyControl}" />
相关问题