2013-06-18 53 views
2

我在Tile控件中設置ControlTemplate(在Telerik TileList中)。它看起來是這樣的:如何設置ControlTemplate的DataContext?

<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}"> 

而且我有,將平鋪的ContentPresenter中呈現的項目定義的DataTemplates。我遇到的麻煩是,當一個ComponentViewModel被添加到ItemsSource(ComponentViewModel ObservableCollection)的目標中時,會出現一個新的Tile,但它的DataContext是RadTileList的ViewModel而不是單個組件的ViewModel。

我是否錯過了關於DataTemplate在ControlTemplate中的設置?

+0

聽起來像是一個telerik bug。遵循WPF的ItemsControl概念,ItemContainers(在這種情況下爲'Tiles')應該將其datacontext設置爲ItemsSource集合中相應的項目。與任何其他基於'ItemsControl'的UI元素完全相同。 –

+0

我已經打開Telerik的門票。我希望不是這樣... –

回答

0

這似乎是這樣做的。我需要爲ContentTemplate和Content屬性執行TemplateBinding。

<ControlTemplate TargetType="{x:Type telerik:Tile}"> 
    <ContentPresenter x:Name="contentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}"/> 

</ControlTemplate> 
0

綁定到附連到DataContext上從一個DataTemplate內的父視圖或控制你必須使用的RelativeSource屬性具有值「FindAncestor」和控制的類型上演示或視圖模型的屬性與您正在尋找的DataContext。

我見過的最常見的錯誤是人們忘記爲AcestorType屬性使用{x:Type yourControlType}標記擴展,並使用「AncestorType = yourControlType」來代替。

下面是一個例子:

Width="{Binding DataContext.SomeProperty, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" 

其中「SomeProperty」是上下面的INotifyPropertyChanged的圖案演示或視圖模型的特性。

寬度是ControlTemplate中控件的屬性

相關問題