Trouver de contrôle à l'intérieur de zone de liste.ItemTemplate (WPF, C#)
J'ai quelques problèmes à trouver le droit TextBlock
de contrôle à l'intérieur d'un StackPanel
.
Balisage:
<ListBox Name="lstTimeline" ItemContainerStyle="{StaticResource TwItemStyle}"
MouseDoubleClick="lstTimeline_MouseDoubleClick">
<ListBox.ItemTemplate>
<DataTemplate>
<DockPanel MaxWidth="{Binding ElementName=lstTimeline, Path=ActualWidth}">
<Border Margin="10" DockPanel.Dock="Left" BorderBrush="White"
BorderThickness="1" Height="48" Width="48" HorizontalAlignment="Center">
<Image Source="{Binding ThumbNail, IsAsync=True}" Height="48" Width="48" />
</Border>
<StackPanel Name="stkPanel" Margin="10" DockPanel.Dock="Right">
<TextBlock Text="{Binding UserName}" FontWeight="Bold" FontSize="18" />
<TextBlock Text="{Binding Text}" Margin="0,4,0,0" FontSize="14"
Foreground="#c6de96" TextWrapping="WrapWithOverflow" />
<TextBlock Text="{Binding ApproximateTime}" FontSize="14"
FontFamily="Georgia" FontStyle="Italic" Foreground="#BBB" />
<TextBlock Text="{Binding ScreenName}" Name="lblScreenName" FontSize="14"
FontFamily="Georgia" FontStyle="Italic" Foreground="#BBB"
Loaded="lblScreenName_Loaded" />
</StackPanel>
</DockPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Mon double cliquez sur le code:
private void lstTimeline_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
ListBoxItem lbi = (lstTimeline.SelectedItem as ListBoxItem);
StackPanel item = lbi.FindName("stkPanel") as StackPanel;
if (item != null)
MessageBox.Show("StackPanel null");
TextBlock textBox = item.FindName("lblScreenName") as TextBlock;
if (textBox != null)
MessageBox.Show("TextBlock null");
MessageBox.Show(textBox.Text);
}
Mais la StackPanel
est null. Comment trouver le bon TextBlock
dans SelectedItem
?
Merci pour votre aide.
- Comment êtes-vous la liaison de la ItemsSource de votre zone de liste? Je ne le vois pas dans le code XAML. Existe-il réellement des éléments dans votre zone de liste? Si non, alors vous obtiendrez toujours une nulle avec le code que vous avez
InformationsquelleAutor | 2009-11-25
Vous devez vous connecter pour publier un commentaire.
Fonction
FindVisualChild
peuvent être trouvés sur la page MSDN FrameworkTemplate.FindName Méthode:Il y a une fonction spécifique à utiliser lorsque vous êtes à la recherche de quelque chose dont le nom est défini dans un modèle. Essayez comme ceci:
Template
?Linq to xml avec un get et set modèle.