views:

332

answers:

1

I need to access element named "PageHost" for the selected list item in the following XAML code from C# codebehind, how to do so please ?

        <ListView.Resources>
            <p:PageWidthConverter x:Key="PageWidthConverter" />
        </ListView.Resources>
        <ListView.ItemTemplate>
            <DataTemplate x:Name="PagesViewDataTemplate">
                <DataTemplate.Resources>
                    <Style x:Key="PageHostStyle" TargetType="{x:Type p:PageHost}">
                        <Setter Property="Width" Value="{Binding Path=ActualWidth, 
                                                                Converter={StaticResource PageWidthConverter}, 
                                                                RelativeSource={RelativeSource Mode=FindAncestor, 
                                                                AncestorType={x:Type Grid}}}"

                                />
                    </Style>
                </DataTemplate.Resources>
                <p:PageHost x:Name="PageHost">

                </p:PageHost>
            </DataTemplate>
        </ListView.ItemTemplate>

        <ListView.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel  Orientation="Horizontal" VerticalAlignment="Top">
                    <WrapPanel.LayoutTransform>
                        <ScaleTransform ScaleX="1" ScaleY="1"></ScaleTransform>
                    </WrapPanel.LayoutTransform>
                </WrapPanel>
            </ItemsPanelTemplate>
        </ListView.ItemsPanel>
        <ListItem>
        </ListItem>
</ListView>
A: 

I guess the below idea will work, though I am not verified the code. You need to find out the ContentPresenter Associated with the SelectedItem first, then using the DataTemplate we can find out the pageHost

 ContentPresenter lstContent = lstViewInstance.ItemContainerGenerator.ContainerFromIndex(lstViewInstance.SelectedIndex) as     ContentPresenter;
 DataTemplate pageViewDataTemplate= this.FindResource("PagesViewDataTemplate") as DataTemplate;
 PageHost pageHost = pageViewDataTemplate.FindName("PageHost", lstContent) as PageHost;
Jobi Joy