【问题标题】:XAML-Binding to Control in ViewModelXAML 绑定到 ViewModel 中的控件
【发布时间】: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,顺便说一句。

标签: c# .net wpf mvvm binding


【解决方案1】:

使用ContentPresenterContentControl

<ContentControl Content="{Binding MyControl}" />

【讨论】:

    猜你喜欢
    • 2013-02-11
    • 2017-12-14
    • 1970-01-01
    • 2015-03-17
    • 2021-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-23
    相关资源
    最近更新 更多