到ContentControl中我有3個的DataTemplate在我Window.Resources,強類型的三種不同的類型:綁定一個DataTemplate與ContentTemplateSelector
<DataTemplate x:Key="myTemplateA" DataType="{x:Type local:SampleClassA}">
<Grid>
<TextBlock Text="{Binding Path=SampleProperty1}"></TextBlock>
<TextBlock Text="{Binding Path=SampleProperty2}"></TextBlock>
<TextBlock Text="{Binding Path=SamplePropertyA}"></TextBlock>
</Grid>
</DataTemplate>
<DataTemplate x:Key="myTemplateB" DataType="{x:Type local:SampleClassB}">
<Grid>
<TextBlock Text="{Binding Path=SampleProperty1}"></TextBlock>
<TextBlock Text="{Binding Path=SampleProperty2}"></TextBlock>
<TextBlock Text="{Binding Path=SamplePropertyB}"></TextBlock>
</Grid>
</DataTemplate>
<DataTemplate x:Key="myTemplateBase" DataType="{x:Type local:SampleClass}">
<Grid>
<TextBlock Text="{Binding Path=SampleProperty1}"></TextBlock>
<TextBlock Text="{Binding Path=SampleProperty2}"></TextBlock>
</Grid>
</DataTemplate>
還有一點DataTemplateSelector:
<local:SampleDataTemplateSelector x:Key="myBindPropertyTemplateSelector"
TemplateA="{StaticResource myTemplateA}"
TemplateB="{StaticResource myTemplateB}"
SampleClassBaseTemplate="{StaticResource myTemplateBase}" />
顯然存在類DataTemplateSelector,稱爲SampleDataTemplateSelector
Public Class SampleDataTemplateSelector
Inherits DataTemplateSelector
Public Property TemplateA As DataTemplate
Public Property TemplateB As DataTemplate
Public Property SampleClassBaseTemplate As DataTemplate
Public Overrides Function SelectTemplate(item As Object, container As DependencyObject) As DataTemplate
If item Is Nothing Then Return SampleClassBaseTemplate
If item.GetType.Equals(GetType(SampleClassA)) Then
Return TemplateA
End If
If item.GetType.Equals(GetType(SampleClassB)) Then
Return TemplateB
End If
Return SampleClassBaseTemplate
End Function
End Class
DataTemplateSelector必須區分3個簡單的類:SampleClass和2個派生類SampleClassA,SampleClassB。
Public Class SampleClass
Public Property SampleProperty1 As String = "Class SampleClass Property1"
Public Property SampleProperty2 As String = "Class SampleClass Property2"
End Class
Public Class SampleClassA
Inherits SampleClass
Public Property SamplePropertyA As String = "Class SampleClassA"
End Class
Public Class SampleClassB
Inherits SampleClass
Public Property SamplePropertyB As String = "Class SampleClassB"
End Class
在我的窗前,XAML中ContentControl中設置類似如下:
<ContentControl Content="{Binding BindProperty}" ContentTemplateSelector="{StaticResource myBindPropertyTemplateSelector}" />
凡BindProperty是隱藏窗口代碼中定義的屬性:
Dim myBindProperty As SampleClass
Public ReadOnly Property BindProperty As SampleClass
Get
Return myBindProperty
End Get
End Property
畢竟,在我的窗口查看事件有BindProperty初始化
Private Sub Window_Loaded(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
myBindProperty = New SampleClassA()
End Sub
沒有編譯錯誤。在執行過程中,SampleDataTemplateSelector的SelectTemplate例程中的item參數始終爲Nothing,因此它不能選擇正確的模板...並不顯示任何內容。我做錯了什麼?
你設置DataContext的地方?在C#中,像這樣:this.DataContext = this; – 2014-09-10 17:06:57
我嘗試在xaml(DataContext =「{Binding local:BindProperty}」)中設置DataContext,並且在代碼後面(myContentControl.DataContext = myBindProperty)中,但沒有任何更改。 – 2014-09-10 17:19:14
Try,myContentControl.DataContext = this(the window)。 myBindProperty不是一個屬性,所以我不認爲你可以使用它(它必須是一個屬性而不是成員變量)。如果你設置了myContentControl.DataContext = BindProperty,那麼你需要將xaml改爲Content =「{Binding}」,因爲上下文已經是BindProperty了。 – 2014-09-10 17:24:59