2012-10-29 3 views
1

J'utilise un ExpanderView pour afficher des données dans mon application. Mais j'ai de la difficulté à essayer de trouver comment obtenir les données d'un ExpanderViewItem après sa sélection.Obtenez l'élément sélectionné sur l'expanderView

Sur un ListBox vous pouvez appeler SelectionChanged = "yourFunction" dans votre code xaml .. mais pour l'expanderview je n'ai aucune idée de comment faire cela?

Ceci est mon code XAML pour l'extension:

<!--Custom header template--> 
    <DataTemplate x:Key="CustomHeaderTemplate"> 
     <TextBlock Text="" FontSize="28" />    
    </DataTemplate> 

    <!--Custom expander template--> 
    <DataTemplate x:Key="CustomExpanderTemplate"> 

     <Grid> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="auto" /> 
      </Grid.RowDefinitions> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition Width="*" /> 
      </Grid.ColumnDefinitions> 

      <Rectangle Width="400" Height="60" Fill="#FFF1F1F1" HorizontalAlignment="Stretch" StrokeThickness="0" Grid.Row="0" Grid.Column="0" /> 
      <TextBlock Text="{Binding procedureName}" FontSize="30" Foreground="#FF00457C" FontWeight="Normal" Grid.Row="0" Grid.Column="0" VerticalAlignment="Center" Margin="10,0,0,0" /> 

     </Grid> 

    </DataTemplate> 

    <!--Custom expander items template--> 
    <DataTemplate x:Key="ExpanderViewItems" > 

     <Grid> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="Auto" /> 
       <RowDefinition Height="Auto" /> 
       <RowDefinition Height="Auto" /> 
       <RowDefinition Height="15" /> 
      </Grid.RowDefinitions> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition Width="auto" /> 
       <ColumnDefinition Width="auto" /> 
       <ColumnDefinition Width="*" /> 
      </Grid.ColumnDefinitions> 

      <Image Source="{Binding flagIcon}" Grid.Row="0" Grid.RowSpan="3" Grid.Column="0" /> 
      <TextBlock FontSize="26" Text="{Binding N}" Foreground="Black" FontWeight="Normal" Grid.Row="0" Grid.Column="1"/> 
      <TextBlock FontSize="20" Text="{Binding RNG}" Foreground="Black" FontWeight="Normal" HorizontalAlignment="Right" Grid.Row="0" Grid.Column="2"/>     
      <TextBlock FontSize="26" Text="{Binding ValueAndUnit}" Foreground="Black" FontWeight="Medium" HorizontalAlignment="Right" Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2"/> 
      <TextBlock FontSize="18" Text="{Binding COM}" Foreground="Black" FontWeight="Normal" Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="2" TextWrapping="Wrap" /> 
      <Line StrokeThickness="1" Stroke="#C4C6CC" Stretch="Fill" X1="0" X2="1" Y1="0" Y2="0" VerticalAlignment="Center" Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="3" /> 
     </Grid> 
    </DataTemplate> 

<!--Listbox Containing ExpanderViews--> 
      <ListBox Name="testsList" Grid.Row="3" Grid.Column="0" > 
       <ListBox.ItemTemplate> 
        <DataTemplate> 

         <!--ExpanderView--> 
         <toolkit:ExpanderView Header="{Binding}"             
               HeaderTemplate="{StaticResource CustomHeaderTemplate}" 
               Expander="{Binding}" 
               ExpanderTemplate="{StaticResource CustomExpanderTemplate}" 
               x:Name="expander" 
               FontSize="36" 
               Foreground="#FF00457C" 
               ItemsSource="{Binding testItems}" 
               ItemTemplate="{StaticResource ExpanderViewItems}" >       
         </toolkit:ExpanderView> 

        </DataTemplate> 
       </ListBox.ItemTemplate> 
      </ListBox> 

J'apprécierais vraiment une aide dans la bonne direction! Cela semble être une question qui n'est pas facile à résoudre sur le web.

Répondre

1

Vous pouvez utiliser l'événement "tap" sur la zone de liste:

Dans votre fichier XAML ajouter un événement de prise listner:

<!--Listbox Containing ExpanderViews--> 
     <ListBox Name="testsList" Grid.Row="3" Grid.Column="0" Tap="testsList_Tap" > 
      <ListBox.ItemTemplate> 
       <DataTemplate> 

        <!--ExpanderView--> 
        <toolkit:ExpanderView Header="{Binding}" 
        ...  

Dans votre code derrière le fichier, mettre en œuvre le gestionnaire du robinet:

private void testsList_Tap(object sender, System.Windows.Input.GestureEventArgs e) 
    { 
     someModel selectedItem = (someModel)this.testsList.SelectedItem; 
     // Do something with your seleted data 
     ... 
    } 
2

@Frederik J'ai implémenté ce que vous avez fait dans le code ci-dessus en utilisant l'événement SelectionChanged de la ListBox - ça marche toujours bien pour moi. Je me suis cogné la tête contre le mur pendant un moment, mais j'ai finalement réussi à le résoudre. Tout d'abord pour la ItemTemplate Je me suis assuré que le modèle est placé dans un élément de ListBoxItem comme suit:

<DataTemplate x:Key="ExpanderViewItems" > 
<ListBoxItem DataContext="{Binding}" Tap="ListBoxItem_Tap_1"> 
<Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto" /> 
      <RowDefinition Height="Auto" /> 
      <RowDefinition Height="Auto" /> 
      <RowDefinition Height="15" /> 
     </Grid.RowDefinitions> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="auto" /> 
      <ColumnDefinition Width="auto" /> 
      <ColumnDefinition Width="*" /> 
     </Grid.ColumnDefinitions> 

     <Image Source="{Binding flagIcon}" Grid.Row="0" Grid.RowSpan="3" Grid.Column="0" /> 
     <TextBlock FontSize="26" Text="{Binding N}" Foreground="Black" FontWeight="Normal" Grid.Row="0" Grid.Column="1"/> 
     <TextBlock FontSize="20" Text="{Binding RNG}" Foreground="Black" FontWeight="Normal" HorizontalAlignment="Right" Grid.Row="0" Grid.Column="2"/>     
     <TextBlock FontSize="26" Text="{Binding ValueAndUnit}" Foreground="Black" FontWeight="Medium" HorizontalAlignment="Right" Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2"/> 
     <TextBlock FontSize="18" Text="{Binding COM}" Foreground="Black" FontWeight="Normal" Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="2" TextWrapping="Wrap" /> 
     <Line StrokeThickness="1" Stroke="#C4C6CC" Stretch="Fill" X1="0" X2="1" Y1="0" Y2="0" VerticalAlignment="Center" Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="3" /> 
    </Grid> 
</ListBoxItem> 
</DataTemplate> 

Une fois que cela est en place, allez dans le code derrière et dans le cas du robinet déclaré pour la ListBoxItem utiliser quelque chose comme ceci:

ListBoxItem item = sender as ListBoxItem; 
ExpanderItemModel model = item.DataContext as ExpanderItemModel; 

Bien sûr, ExpanderItemModel sera ce que vous utilisez pour vos articles d'extension ...

cela a bien fonctionné pour moi

J'espère que cela aide!

Bonne chance!

0

Vous pouvez obtenir les valeurs sélectionnées par sélection de la liste déroulante ou événements étendus de présentation. Pour listbox:

private void lstExams_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
     if (e.AddedItems.Count > 0) 
     { 
      Model.ExamTitles data = (sender as ListBox).SelectedItem as Model.ExamTitles; 
      } 
    } 

Ici ExamTitles est une classe qui contient des collections

Pour expanderview élargi

private void ExpanderView_Expanded(object sender, RoutedEventArgs e) 
    { 
     ExpanderView expview = (sender as ExpanderView); 
     Model.ExamTitles data = expview.Header as Model.ExamTitles; 
    } 

Hope this helps!

Questions connexes