2016-03-01 23 views
-2

我有一個列表對象(PropertyBaseKeyValue屬性的基本類型),我想以某種形式的格式向用戶顯示。基於對象類型,我想在不同的控件之間切換。假設int,double值的值爲Label s,其中string的值可通過TextBox進行編輯。同樣,我想爲enum值顯示ComboBoxWPF - 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> 

回答

0

問題是數據綁定。 ContentPresenter未正確綁定。 <ContentPresenter Grid.Column="1" Content="{Binding}">綁定到當前源(ref)。當前源是PropertyBase類,其中包含KeyValueContentPresenter正試圖通過調用ToString方法而不是使用Value來顯示PropertyBase類我想介紹。

使用<ContentPresenter Grid.Column="1" Content="{Binding Path=Value}">解決了這個問題。