[英]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.