English 中文(简体)
Find control inside Listbox.ItemTemplate (WPF C#)
原标题:

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>

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. How do find the right TextBlock in SelectedItem?

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:

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.

var item = ...

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




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签