如何在WPF中查找由ListViewItem DataTemplate生成的元素

时间:2012-10-31 11:39:52

标签: c# wpf listview

我在内置CheckBox的WPF应用程序中有一个ListView。

我通过DataTable用ChapterID和ChapterTitles

填充ListView
<ListView x:Name="listViewChapter" IsSynchronizedWithCurrentItem="True" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" BorderThickness="0" SelectionMode="Multiple" Margin="0,2,-1,3" TabIndex="17" Grid.ColumnSpan="2">
                <ListView.GroupStyle>
                    <GroupStyle>
                        <GroupStyle.Panel>
                            <ItemsPanelTemplate>
                                <UniformGrid Columns="3" />
                            </ItemsPanelTemplate>
                        </GroupStyle.Panel>
                    </GroupStyle>
                </ListView.GroupStyle>
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Vertical" HorizontalAlignment="Left" VerticalAlignment="Center">
                            <CheckBox Name="chkChapterTitle" VerticalAlignment="Center" Margin="0,0,0,0" Tag="{Binding ChapterID}" Content="{Binding ChapterTittle}" Checked="chkChapterTitle_Checked" Unchecked="chkChapterTitle_Unchecked" IsChecked="{Binding RelativeSource={RelativeSource AncestorType=ListViewItem},Path=IsSelected}" />
                        </StackPanel>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

现在我想访问CheckBoxes内部检查...

我的代码是......

        private void ToCheckChapters()
    {
        for (int i = 0; i < listViewChapter.Items.Count; i++)
        {

            // Get a all list items from listbox
            ListViewItem ListBoxItemObj = (ListViewItem)listViewChapter.ItemContainerGenerator.ContainerFromItem(listViewChapter.Items[i]);
            //bool check = ListBoxItemObj.HasContent;
            // find a ContentPresenter of that list item.. [Call FindVisualChild Method]
            ContentPresenter ContentPresenterObj = FindVisualChild<ContentPresenter>(ListBoxItemObj);

                // call FindName on the DataTemplate of that ContentPresenter
                DataTemplate DataTemplateObj = ContentPresenterObj.ContentTemplate;
                CheckBox Chk = (CheckBox)DataTemplateObj.FindName("chkChapterTitle", ContentPresenterObj);
                Chk.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;
    }

但在此代码中,ContentPresenterObj始终为null ...

1 个答案:

答案 0 :(得分:1)

我自己得到了答案..

我必须在ListView加载后调用ToCheckChapters()函数...