我有一個列表對象(PropertyBase
是Key
和Value
屬性的基本類型),我想以某種形式的格式向用戶顯示。基於對象類型,我想在不同的控件之間切換。假設int
,double
值的值爲Label
s,其中string
的值可通過TextBox
進行編輯。同樣,我想爲enum
值顯示ComboBox
。WPF - Templatize基於類型的對象列表
到目前爲止,我已閱讀關於DataTemplate
s,ContentPresenter
s,並拿出以下xaml代碼片。但是,下面的模板顯示對象的類型(PropertyBase[Int64]
,PropertyBase[String]
),而不是它的值。那有什麼問題?
<ItemsControl ItemsSource="{Binding Path=Properties}">
<ItemsControl.ItemTemplate>
<DataTemplate DataType="models:PropertyBase">
<Grid Margin="0,0,0,5">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Content="{Binding Key}" />
<ContentPresenter Grid.Column="1" Content="{Binding}">
<ContentPresenter.Resources>
<DataTemplate DataType="{x:Type system:Int64}">
<Label Content="{Binding}" />
</DataTemplate>
<DataTemplate DataType="{x:Type system:String}">
<TextBox Text="{Binding Value, Mode=TwoWay}" />
</DataTemplate>
</ContentPresenter.Resources>
</ContentPresenter>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>