所以我要在UserControl中定义一个DataTemplate。资源如下:
override func viewDidLoad() {
super.viewDidLoad()
// Configure Navigation Bar So it is transparent
if let navBar = self.navigationController?.navigationBar {
let image = UIImage()
navBar.setBackgroundImage(image, for: .default)
navBar.shadowImage = image
navBar.isTranslucent = true
navBar.tintColor = UIColor.black
// Only Back Arrow without Back text
navBar.topItem!.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
}
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
showPopup()
}
func showPopup() -> Void {
let popupVC = PopupViewController()
self.popupViewController = popupVC
popupVC.delegate = self
self.addChildViewController(popupVC)
self.view.addSubview(popupVC.view)
NSLayoutConstraint.autoCreateAndInstallConstraints {
popupVC.view.autoPin(toTopLayoutGuideOf: self, withInset: 16)
popupVC.view.autoPinEdge(toSuperviewEdge: .leading, withInset: 16)
popupVC.view.autoPinEdge(toSuperviewEdge: .trailing, withInset: 16)
}
self.view.layoutIfNeeded()
popupVC.didMove(toParentViewController: self)
}
然后在可视树中有一个ItemsControl定义,如下所示:
<DataTemplate x:Key="Impact">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0"
Text="Impact:" />
<ComboBox Grid.Column="1"
ItemsSource="{Binding Impacts}">
</ComboBox>
</Grid>
</DataTemplate>
是否有一种方法可以将资源中的<ItemsControl ItemsSource="{Binding Path=ViewModelsList}">
<ItemsControl.Resources>
<DataTemplate DataType="{x:Type ui:FirstViewModel}">
<ContentControl ContentTemplate="{StaticResource Impact}"></ContentControl>
</DataTemplate>
<DataTemplate DataType="{x:Type ui:SecondViewModel}">
<ContentControl ContentTemplate="{StaticResource Impact}"></ContentControl>
...
</DataTemplate>
</ItemsControl.Resources>
</ItemsControl>
与DataTemplate的DataType中定义的各个ViewModel类型相关联?
也许有一种方法可以将DataType传递给ContentControl?
答案 0 :(得分:0)
将此添加到您的ContentControls:
Content="{Binding}"