Find control inside Listbox.ItemTemplate (WPF C#)(在 Listbox.ItemTemplate (WPF C#) 中查找控件)
问题描述
我在 StackPanel 中找到正确的 TextBlock 控件时遇到了一些问题.我的标记:
I have some problems finding the right TextBlock control inside a StackPanel. 
My markup:
<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?
But the StackPanel is null.  How do find the right TextBlock in SelectedItem?
感谢您的帮助.
推荐答案
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 方法:
Function FindVisualChild can be found on the MSDN page 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;
}
                        这篇关于在 Listbox.ItemTemplate (WPF C#) 中查找控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Listbox.ItemTemplate (WPF C#) 中查找控件
				
        
 
            
        - 输入按键事件处理程序 2022-01-01
 - 如何用自己压缩一个 IEnumerable 2022-01-01
 - C# 中多线程网络服务器的模式 2022-01-01
 - 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
 - WebMatrix WebSecurity PasswordSalt 2022-01-01
 - 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
 - C#MongoDB使用Builders查找派生对象 2022-09-04
 - 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
 - Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
 - MoreLinq maxBy vs LINQ max + where 2022-01-01
 
						
						
						
						
						
				
				
				
				