2009-11-25 34 views
6

Tôi gặp một số vấn đề khi tìm điều khiển bên phải TextBlock bên trong một StackPanel. đánh dấu của tôi:Tìm điều khiển bên trong 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> 

My đôi đang nhấp:

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); 
} 

Nhưng StackPanel là null. Làm cách nào để tìm đúng số TextBlock trong SelectedItem?

Cảm ơn sự giúp đỡ của bạn.

+0

How are you ràng buộc ItemsSource của ListBox của bạn? Tôi không thấy nó được đặt trong XAML. Có thực sự các mục trong ListBox của bạn không? Nếu không thì bạn sẽ luôn nhận được một null với mã bạn có –

Trả lời

0

Có một chức năng cụ thể để sử dụng khi bạn đang tìm kiếm thứ gì đó có tên được xác định trong mẫu. Hãy thử nó như sau:

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 trả về null (Giá trị = null) –

+0

Mã không đầy đủ. 'Template' là gì? – GONeale

+0

Mẫu trong đoạn mã trên là this.Template, hoặc ListBox.Template - mặc dù nó có thể là bất kỳ đối tượng nào có mẫu. –

0

LINQ to xml với mô hình nhận và đặt.

var item = ... 

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

Tôi đã giải quyết vấn đề này: 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; 
} 

Chức năng FindVisualChild có thể được tìm thấy trên MSDN trang 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

Cảm ơn, Simon. Điều đó có hiệu quả đối với tôi, nhưng tôi đã mất khá nhiều thời gian để tìm ra rằng "FindVisualChild" là một phương pháp bạn phải tự viết: http://msdn.microsoft.com/en-us/library/bb613579.aspx –

+1

@ BrianSchroer Tôi đã cập nhật câu trả lời với phương thức liên kết – sergtk

+1

findName của bạn không có sẵn trong Windows Phone ?? –

Các vấn đề liên quan