Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Listbox.ItemTemplate(WPF C#)中查找控件_C#_Wpf_Listbox_Mouseevent_Selecteditem - Fatal编程技术网

在Listbox.ItemTemplate(WPF C#)中查找控件

在Listbox.ItemTemplate(WPF C#)中查找控件,c#,wpf,listbox,mouseevent,selecteditem,C#,Wpf,Listbox,Mouseevent,Selecteditem,在StackPanel中查找正确的TextBlock控件时遇到一些问题。 我的标记: <ListBox Name="lstTimeline" ItemContainerStyle="{StaticResource TwItemStyle}" MouseDoubleClick="lstTimeline_MouseDoubleClick"> <ListBox.ItemTemplate> <DataTemplate>

StackPanel
中查找正确的
TextBlock
控件时遇到一些问题。 我的标记:

<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>
但是
StackPanel
为空。如何在
SelectedItem
中找到正确的
TextBlock


感谢您的帮助。

当您查找模板中定义了名称的内容时,可以使用一个特定的函数。试着这样做:

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

使用get和set模型将Linq转换为xml

var item = ...

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

StackPanel返回空(值=空)代码不完整。什么是
Template
?上面代码中的模板是this.Template或ListBox.Template——尽管它可以是任何带有模板的对象。我用以下方法解决了它:如何绑定ListBox的ItemsSource?我没有看到它被设置在XAML中。您的列表框中确实有项目吗?如果不是这样,那么你的代码将始终为空。谢谢,西蒙。这对我来说非常有效,但我花了很长时间才意识到“FindVisualChild”是一种你必须自己编写的方法:findName方法在Windows Phone中不可用??
var item = ...

            lstTimeline.SelectedIndex = -1;
            lstTimeline.ItemsSource = item;
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;
}
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;
}