2009-11-25 14 views
6

के अंदर नियंत्रण पाएं मुझेके अंदर दाएं TextBlock नियंत्रण खोजने में कुछ समस्याएं हैं। मेरे मार्कअप: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> 

मेरे डबल क्लिक करें कोड:

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

लेकिन StackPanel रिक्त है। SelectedItem में सही TextBlock कैसे खोजें?

आपकी मदद के लिए धन्यवाद।

+0

आप अपने ListBox के ItemsSource बाध्यकारी हैं? मुझे यह एक्सएएमएल में सेट नहीं दिख रहा है। क्या वास्तव में आपके लिस्टबॉक्स में आइटम हैं? यदि नहीं तो आपको हमेशा –

उत्तर

0

जब आप किसी ऐसे टेम्पलेट में परिभाषित किए गए किसी चीज़ की तलाश में हैं तो इसका उपयोग करने के लिए एक विशिष्ट फ़ंक्शन है। इसे इस तरह आज़माएं:

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

कोड के साथ शून्य मिल जाएगा, स्टैकपैनल शून्य (वैल्यू = शून्य) –

+0

कोड अपूर्ण है। 'टेम्पलेट' क्या है? – GONeale

+0

उपर्युक्त कोड में टेम्पलेट यह है। टेम्पलेट, या ListBox.Template - हालांकि यह टेम्पलेट के साथ कोई ऑब्जेक्ट हो सकता है। –

0

लिंक और सेट मॉडल के साथ लिंक करने के लिए लिंक।

var item = ... 

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

मैंने इसे हल किया: 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; 
} 

समारोह FindVisualChild 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

धन्यवाद, साइमन। यह मेरे लिए बहुत अच्छा काम करता था, लेकिन मुझे यह पता लगाने में काफी समय लगा कि "FindVisualChild" एक तरीका है जिसे आपको स्वयं लिखना है: http://msdn.microsoft.com/en-us/library/bb613579.aspx –

+1

@ BrianSchroer मैंने आपके लिंक – sergtk

+1

के साथ उत्तर अपडेट किया है findName विधि विंडोज फोन में उपलब्ध नहीं है ?? –