簡體   English   中英

將整個UserControl綁定到屬性

[英]Binding entire UserControl to a property

我的viewModel從服務獲取UserControl。 我想顯示該UserControl。

XAML看起來像:

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

    </ContentPresenter>
</Grid>

而ViewModel的屬性如下:

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

因此,我獲取ui並將其設置為該VM的屬性。 但是,UI中沒有任何內容。 MyUI屬性的getter永遠不會被調用,即使在我給它賦值之后也是如此:

MyUI = 'some user control';

VM不會包含類似UI的特定元素。 但是,它將為MyUI屬性檢索ViewModel上下文,並在XAML使用DataTemplate適當地呈現該ViewModel Type

實現INotifyPropertyChanged並在MyUI屬性set通知MyUI屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM