2009-11-25 5 views
6

J'ai quelques problèmes à trouver le bon TextBlock contrôle à l'intérieur d'un StackPanel. Mon balisage:Trouver le contrôle dans Listbox.ItemTemplate (WPF C#)

<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 code de clic:

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 nulle. Comment trouver le droit TextBlock dans SelectedItem?

Merci pour votre aide.

+0

Comment vous liez la ItemsSource de votre ListBox? Je ne vois pas qu'il est défini en XAML. Y a-t-il des éléments dans votre ListBox? Si ce n'est pas le cas, vous obtiendrez toujours une valeur nulle avec le code que vous avez –

Répondre

0

Il existe une fonction spécifique à utiliser lorsque vous cherchez quelque chose dont le nom est défini dans un modèle. Essayez-le comme ceci:

private void lstTimeline_MouseDoubleClick(object sender, MouseButtonEventArgs e) 
{ 
    ListBoxItem lbi = (lstTimeline.SelectedItem as ListBoxItem); 

    StackPanel item = Template.FindName("stkPanel",lbi) as StackPanel; 
    if (item != null) 
     MessageBox.Show("StackPanel null"); 
    TextBlock textBox = Template.FindName("lblScreenName",item) as TextBlock; 
    if (textBox != null) 
     MessageBox.Show("TextBlock null"); 

    MessageBox.Show(textBox.Text); 
} 
+0

StackPanel renvoie null (Value = null) –

+0

Code incomplet. Qu'est-ce que 'Template'? – GONeale

+0

Le modèle dans le code ci-dessus est this.Template, ou ListBox.Template - bien qu'il puisse s'agir de n'importe quel objet avec un modèle. –

0

Linq à xml avec un modèle get et set.

var item = ... 

      lstTimeline.SelectedIndex = -1; 
      lstTimeline.ItemsSource = item; 
+1

Je l'ai résolu avec ceci: http://social.msdn.microsoft.com/forums/en-US/wpf/thread/7d2b4134-e460-4daa-86b7-24a629d77718 –

10
ListBoxItem myListBoxItem = (ListBoxItem)(lstUniqueIds.ItemContainerGenerator.ContainerFromIndex(lstUniqueIds.SelectedIndex)); 
ContentPresenter myContentPresenter = FindVisualChild<ContentPresenter>(myListBoxItem); 
DataTemplate myDataTemplate = myContentPresenter.ContentTemplate; 
CheckBox target = (CheckBox)myDataTemplate.FindName("chkUniqueId", myContentPresenter); 
if (target.IsChecked) 
{ 
    target.IsChecked = false; 
} 
else 
{ 
    target.IsChecked = true; 
} 

Fonction FindVisualChild se trouve sur la page MSDN FrameworkTemplate.FindName Method:

private childItem FindVisualChild<childItem>(DependencyObject obj) 
    where childItem : DependencyObject 
{ 
    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++) 
    { 
     DependencyObject child = VisualTreeHelper.GetChild(obj, i); 
     if (child != null && child is childItem) 
      return (childItem)child; 
     else 
     { 
      childItem childOfChild = FindVisualChild<childItem>(child); 
      if (childOfChild != null) 
       return childOfChild; 
     } 
    } 
    return null; 
} 
+2

Merci, Simon. Cela a fonctionné très bien pour moi, mais il m'a fallu un certain temps pour comprendre que "FindVisualChild" est une méthode que vous devez écrire vous-même: http://msdn.microsoft.com/en-us/library/bb613579.aspx –

+1

@ BrianSchroer J'ai mis à jour la réponse avec votre lien – sergtk

+1

findName méthode n'est pas disponible dans Windows Phone ?? –

Questions connexes