2009-11-25 26 views
6

右のTextBlockコントロールを見つけるにはいくつか問題があります。StackPanelです。 私のマークアップ: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がnullです。 TextBlockSelectedItemにどのようにして見つけられますか?

ありがとうございました。

+0

どのようにあなたのListBoxのItemsSourceの結合されていますか?私はそれがXAMLに設定されて表示されません。 ListBoxに実際に項目がありますか?そうでなければ、あなたは常にあなたが持っているコードでヌルを取得します –

答えて

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

StackPanelはnullを返します(値= null) –

+0

コードが不完全です。 'Template'とは何ですか? – GONeale

+0

上記のコード内のテンプレートはThis.Template、またはListBox.Templateですが、テンプレートを持つオブジェクトでも可能です。 –

0

getとsetモデルでLinqとxml。

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メソッドはWindows Phoneで利用できませんか? –

関連する問題