C# ViewModel中控件的XAML绑定
ViewModel类:C# ViewModel中控件的XAML绑定,c#,.net,wpf,mvvm,binding,C#,.net,Wpf,Mvvm,Binding,ViewModel类: class MyViewModel : ViewModelBase, INotifyPropertyChanged { public string SomeText { get{...} set {...}} public AnyNonMVVMControl MyControl { get {...} set {...}} ... } 窗口XAML: <Grid> ... <TextBlock Text="{Binding So
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>
我有一个控件,它不是为MVVM设计的。渲染和数据是强耦合的(糟糕的设计解决方案)。我的viewmodel中有此控件,希望将其绑定到窗口。但是我不想直接在XAML中添加该控件,因为该控件还包含业务逻辑和数据->我需要在viewmodel中访问它以执行操作
那么,如何通过绑定将控件“添加”到窗口?使用
ContentPresenter
或ContentControl
:
<ContentControl Content="{Binding MyControl}" />
使用
ContentPresenter
或ContentControl
:
<ContentControl Content="{Binding MyControl}" />
从视图模型公开控件不是MVVM,顺便说一句。从视图模型公开控件不是MVVM,顺便说一句。