![](/img/trans.png)
[英]Correct design for binding data to a XAML control in a view, from the ViewModel
[英]XAML-Binding to Control in ViewModel
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(...);
...
我有一個不是為MVVM設計的控件。 渲染和數據緊密耦合(不良的設計解決方案)。 我的視圖模型中有此控件,並想將其綁定到我的窗口。 但是我不想直接在XAML中添加控件,因為該控件還包含業務邏輯和數據->我需要在視圖模型中訪問它以執行操作。
那么,如何通過綁定“添加”控件到窗口?
使用ContentPresenter
或ContentControl
:
<ContentControl Content="{Binding MyControl}" />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.