简体   繁体   中英

how to find elements that are generated by a ListViewItem DataTemplate in WPF

I have a ListView in WPF Application with CheckBox inside.

I am filling that ListView by DataTable with ChapterID and ChapterTitles

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

Now i want to access inside CheckBoxes checked that...

My Code is...

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

But in this code ContentPresenterObj is always null...

I got the answer myself..

I have to call ToCheckChapters() function after ListView loaded...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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