我正在为Tile控件(在Telerik TileList中)设置ControlTemplate。它看起来像这样:
<ControlTemplate TargetType="{x:Type telerik:Tile}">
<Border>
<!-- Some Content that binds to DP on the view models -->
<ContentPresenter Content="{Binding}" />
</Border>
</ControlTemplate>
其他地方:
<telerik:RadTileList ItemsSource="{Binding ComponentViewModels}">
我已经为Tile的ContentPresenter中呈现的项目定义了DataTemplates。 我遇到的问题是,当将ComponentViewModel添加到ItemsSource(ComponentViewModel ObservableCollection)的目标中时,会出现一个新的Tile,但它的DataContext是RadTileList的ViewModel而不是单个组件的ViewModel。
在ControlTemplate中设置DataContext时是否漏掉了什么?
ItemsControl
哲学,ItemContainers
(在这种情况下是Tiles
)应该将它们的数据上下文设置为ItemsSource
集合中相应的项。就像任何其他基于ItemsControl
的UI元素一样。 - Federico Berasategui