locked
how to access a cell (row/col) in a data bound listview with data templates RRS feed

  • Question

  • I have an application that has listview bound to a dataset and a gridview that has a number of data templates.

    What I need to be able to do is access a specific ui element based upon the row and the column.  


    some code snippets as follows:

                 ListViewItem lvi = lv.ItemContainerGenerator.ContainerFromIndex(lv.SelectedIndex) as ListViewItem;

                ContentPresenter myContentPresenter = FindVisualChild<ContentPresenter>(lvi);

                 DataTemplate myDataTemplate = myContentPresenter.ContentTemplate;

                TextBlock myTextBlock = (TextBlock)myDataTemplate.FindName("SequenceTemplate", myContentPresenter);
                  


    but myTextBlock is always null



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


    and the xaml snippets:



    <DataTemplate x:Key="SequenceTemplate">
                    <Border Style="{StaticResource GridLeftColumn}">
                        <StackPanel Style="{StaticResource GridStackPanel}" >                       
                            <TextBlock Name="lblseq" Text="{Binding Path=Seq}"
                                       HorizontalAlignment="Right"
                                       />
                            <TextBox Name="txtbseq" Text="{Binding Path=Seq}" HorizontalAlignment="Right" HorizontalContentAlignment="Right"
                                     Style="{DynamicResource textboxEdit}" IsReadOnly="True"
                                     BorderThickness="0" GotMouseCapture="click_RightMouse" PreviewMouseDown="click_RightMouse"  Visibility="Collapsed"
                                     Margin="-2,-2,-4,-2"
                                     GotFocus="nonentrygotfocus" LostFocus="nonentrylostfocus" PreviewKeyDown="textnondroppreview" Uid="SeqColName"/>
                            <TextBox Name="prtDataRow" Text="{Binding Path=ID}" Visibility="Collapsed"/>
                    </StackPanel>
                    </Border>
                    <DataTemplate.Triggers>
                        <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor,
                                    AncestorType={x:Type ListBoxItem}}, Path=IsSelected}" Value="True">
                            <Setter TargetName="txtbseq" Property="Visibility" Value="Visible"/>
                            <Setter TargetName="lblseq" Property="Visibility" Value="Collapsed"/>
                        </DataTrigger>
                    </DataTemplate.Triggers>
                </DataTemplate>



    <Grid >
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <Grid Name="entryGrid"  Grid.Column="1">
                    <ListView ScrollViewer.CanContentScroll="True"
                      ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Auto"
                DataContext="{Binding ElementName=liEntryWindow, Path=lineItemsTable}"
                ItemsSource="{Binding LineItems}"
                ItemContainerStyleSelector="{StaticResource ListViewItemStyleSelector}"
                Name="lv" Background="Transparent"
                SizeChanged="listviewSizeChanged"  LayoutUpdated="layoutupdated"
                              Extensions:ListViewColumns.Stretch="true"
                      >
                        <ListView.View>
                            <GridView AllowsColumnReorder="False" x:Name="ligrdvw">
                                <GridView.Columns>
                                    <GridViewColumn x:Name="grdSeq" Header="Seq" Width="30"
                                            CellTemplate="{StaticResource SequenceTemplate}"/>

    Monday, January 12, 2009 3:31 AM

Answers

All replies