<bdo id='FZWwY'></bdo><ul id='FZWwY'></ul>

    <tfoot id='FZWwY'></tfoot>
  1. <i id='FZWwY'><tr id='FZWwY'><dt id='FZWwY'><q id='FZWwY'><span id='FZWwY'><b id='FZWwY'><form id='FZWwY'><ins id='FZWwY'></ins><ul id='FZWwY'></ul><sub id='FZWwY'></sub></form><legend id='FZWwY'></legend><bdo id='FZWwY'><pre id='FZWwY'><center id='FZWwY'></center></pre></bdo></b><th id='FZWwY'></th></span></q></dt></tr></i><div id='FZWwY'><tfoot id='FZWwY'></tfoot><dl id='FZWwY'><fieldset id='FZWwY'></fieldset></dl></div>

    <small id='FZWwY'></small><noframes id='FZWwY'>

    <legend id='FZWwY'><style id='FZWwY'><dir id='FZWwY'><q id='FZWwY'></q></dir></style></legend>
    1. 在 Listbox.ItemTemplate (WPF C#) 中查找控件

      时间:2023-10-07
      <tfoot id='mcrwc'></tfoot>

      <small id='mcrwc'></small><noframes id='mcrwc'>

            <i id='mcrwc'><tr id='mcrwc'><dt id='mcrwc'><q id='mcrwc'><span id='mcrwc'><b id='mcrwc'><form id='mcrwc'><ins id='mcrwc'></ins><ul id='mcrwc'></ul><sub id='mcrwc'></sub></form><legend id='mcrwc'></legend><bdo id='mcrwc'><pre id='mcrwc'><center id='mcrwc'></center></pre></bdo></b><th id='mcrwc'></th></span></q></dt></tr></i><div id='mcrwc'><tfoot id='mcrwc'></tfoot><dl id='mcrwc'><fieldset id='mcrwc'></fieldset></dl></div>
            <legend id='mcrwc'><style id='mcrwc'><dir id='mcrwc'><q id='mcrwc'></q></dir></style></legend>

              <bdo id='mcrwc'></bdo><ul id='mcrwc'></ul>
                  <tbody id='mcrwc'></tbody>

                本文介绍了在 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#) 中查找控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:在列表框中设置所选项目而不循环 下一篇:WPF列表框通过单击空白区域删除选择

                相关文章

                <i id='QwwiO'><tr id='QwwiO'><dt id='QwwiO'><q id='QwwiO'><span id='QwwiO'><b id='QwwiO'><form id='QwwiO'><ins id='QwwiO'></ins><ul id='QwwiO'></ul><sub id='QwwiO'></sub></form><legend id='QwwiO'></legend><bdo id='QwwiO'><pre id='QwwiO'><center id='QwwiO'></center></pre></bdo></b><th id='QwwiO'></th></span></q></dt></tr></i><div id='QwwiO'><tfoot id='QwwiO'></tfoot><dl id='QwwiO'><fieldset id='QwwiO'></fieldset></dl></div>

                <small id='QwwiO'></small><noframes id='QwwiO'>

                  <legend id='QwwiO'><style id='QwwiO'><dir id='QwwiO'><q id='QwwiO'></q></dir></style></legend>
                  <tfoot id='QwwiO'></tfoot>

                  • <bdo id='QwwiO'></bdo><ul id='QwwiO'></ul>