简体   繁体   English

将整个UserControl绑定到属性

[英]Binding entire UserControl to a property

My viewModel gets a UserControl from a service. 我的viewModel从服务获取UserControl。 I want to display that UserControl. 我想显示该UserControl。

XAML looks like: XAML看起来像:

<Grid >
    <ContentPresenter Content="{Binding AddInUI}">

    </ContentPresenter>
</Grid>

And the ViewModel's property like: 而ViewModel的属性如下:

public UIElement MyUI
        {
            get
            {
                return myUI;
            }
            set
            {
                Set(ref myUI, value);
            }
        }

So I fetch the ui and set to this VM's property. 因此,我获取ui并将其设置为该VM的属性。 However, nothing renders in the UI. 但是,UI中没有任何内容。 The getter of the MyUI property never gets called, even after I assign value to it, like: MyUI属性的getter永远不会被调用,即使在我给它赋值之后也是如此:

MyUI = 'some user control';

A VM would not contain a UI-specific element like this. VM不会包含类似UI的特定元素。 It would however retrieve a ViewModel context for MyUI property and use a DataTemplate in the XAML to render that ViewModel Type appropriately. 但是,它将为MyUI属性检索ViewModel上下文,并在XAML使用DataTemplate适当地呈现该ViewModel Type

实现INotifyPropertyChanged并在MyUI属性set通知MyUI属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM