2017-02-02 4 views
1

Ich habe einige Probleme mit der Bindung von ContentControl an ListboxItem.Set DataTemplate von ElementName

Das ist meine ListBox.

<ListBox x:Name="box"> 
    <ListBoxItem Tag="{x:Type vm:PART_FAMILY_ViewModel}" Content="Car"></ListBoxItem> 
    <ListBoxItem Tag="{x:Type vm:PART_TYPES_ViewModel}" Content="Bike" IsSelected="True"></ListBoxItem> 
</ListBox> 

Das ist mein ContenControl:

<ContentControl Content="{Binding ElementName=box, Path=SelectedItem.Tag}"> 
    <ContentControl.Resources> 

    <DataTemplate DataType="{x:Type vm:PART_FAMILY_ViewModel}"> 
     <v:PART_FAMILYS_View></v:PART_FAMILYS_View> 
    </DataTemplate> 

    <DataTemplate DataType="{x:Type vm:PART_TYPES_ViewModel}"> 
     <v:PART_TYPES_View></v:PART_TYPES_View> 
    </DataTemplate> 

    </ContentControl.Resources> 
</ContentControl> 

Mit meinem Elementname ich den Tag Datatype in meinem Content nur sehen.

Antwort

1

Mit meinem Elementname sehe ich nur den Tag DataType in meinem ContentControl.

Das liegt daran, dass Sie die Tag-Eigenschaft auf Typ festlegen. Sie sollten es zu einer Instanz des Typs Set für die Datatemplate wie erwartet angewendet werden:

<ListBox x:Name="box"> 
    <ListBoxItem Content="Car"> 
     <ListBoxItem.Tag> 
      <vm:PART_FAMILY_ViewModel /> 
     </ListBoxItem.Tag> 
    </ListBoxItem> 
    <ListBoxItem Content="Bike" IsSelected="True"> 
     <ListBoxItem.Tag> 
      <vm:PART_TYPES_ViewModel /> 
     </ListBoxItem.Tag> 
    </ListBoxItem> 
</ListBox> 
Verwandte Themen