简体   繁体   English

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

[英]Find control inside Listbox.ItemTemplate (WPF C#)

I have some problems finding the right TextBlock control inside a StackPanel . 我在StackPanel找到正确的TextBlock控件时遇到了一些问题。 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>

My double click code: 我的双击代码:

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

But the StackPanel is null. StackPanel为空。 How do find the right TextBlock in SelectedItem ? 如何在SelectedItem找到正确的TextBlock

Thanks for your help. 谢谢你的帮助。

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

Function FindVisualChild can be found on the MSDN page FrameworkTemplate.FindName Method : 函数FindVisualChild可以在MSDN页面上找到FrameworkTemplate.FindName方法

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

There's a specific function to use when you're looking for something whose name is defined in a template. 当您在寻找名称在模板中定义的内容时,可以使用特定的函数。 Try it like this: 试试这样:

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

Linq to xml with a get and set model. Linq to xml带有get和set模型。

var item = ...

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM