<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#) 中查找控件

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

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Populate ListBox with a IEnumrable on another thread (winforms)(在另一个线程(winforms)上使用 IEnumrable 填充 ListBox)
                listbox selected item give me quot; System.Data.DataRowViewquot; , C# winforms(列表框选择的项目给我quot;System.Data.DataRowView, C# Winforms)
                Cannot remove items from ListBox(无法从列表框中删除项目)
                Preventing ListBox scrolling to top when updated(更新时防止列表框滚动到顶部)
                Drag and drop from list to canvas on windows phone with MVVM(使用 MVVM 在 Windows 手机上从列表拖放到画布)
                Deselection on a WPF listbox with extended selection mode(具有扩展选择模式的 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>
                        <tbody id='QwwiO'></tbody>

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