2009-04-21 8 views
2

Tenir compte du contrôle/ModèleComment lier à la propriété SelectedItem dans un ControlTemplate?

<my:ExpandingListBox Margin="0,2,0,0" x:Name="TagSearchListBox"> 
    <my:ExpandingListBox.Template> 
     <ControlTemplate TargetType="{x:Type my:ExpandingListBox}"> 
      <Border Name="MyBorder" BorderThickness="2" BorderBrush="Black"> 
       <Grid> 
        <TextBlock Name="MySelectionInfo" Background="White" Text="{TemplateBinding SelectedItem}"/> 
        <ScrollViewer Name="MyScrollViewer" HorizontalScrollBarVisibility="Hidden" Opacity="0" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" VerticalScrollBarVisibility="Hidden"> 
         <ItemsPresenter Name="MyItemsPresenter"/> 
        </ScrollViewer> 
       </Grid> 
      </Border> 
     </ControlTemplate> 
    </my:ExpandingListBox.Template> 
</my:ExpandingListBox> 
suivant

Fondamentalement, il y a des déclencheurs/des ressources supplémentaires qui causent le contrôle à développer/effondrement quand IsMouseOver est vrai. Lorsque le contrôle est réduit, j'aimerais que le TextBlock "MySelectionInfo" affiche le texte de l'élément sélectionné; quand il est développé, j'aimerais que la liste des éléments soit affichée comme d'habitude. Y at-il un moyen d'attraper le texte de l'élément sélectionné et l'afficher dans le TextBlock en pur XAML?

La définition de Text = "{TemplateBinding SelectedItem}" s'exécute, mais n'affiche rien.

EDIT (Solution):

<TextBlock Name="MySelectionInfo" Background="White"> 
    <TextBlock.Text> 
     <Binding Path="SelectedItem.Name"> 
      <Binding.RelativeSource> 
       <RelativeSource Mode="FindAncestor" AncestorType="{x:Type my:ExpandingListBox}"/> 
      </Binding.RelativeSource> 
     </Binding> 
    </TextBlock.Text> 
</TextBlock> 

".Nom" est une propriété connue du type d'article que je veux afficher.

Répondre

2

Cela fonctionnerait-il à lier en utilisant un RelativeSource à la place? Peut-être quelque chose comme ceci:

<TextBlock Name="MySelectionInfo" Background="White" 
    Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type my:ExpandingListBox}}}" /> 
+0

A travaillé super! Voir ci-dessus pour la solution :) – Pwninstein