locked
How to get ContentTemplateRoot of ItemContainer by Item on list view? RRS feed

  • Question

  • How to get ContentTemplateRoot of ItemContainer by Item on list view?

    it is possible to access Item and ContentTemplateRoot in ContainerContentChanging event on listview like this code

    --------------------------------------------------------------------------------------------------------------------------------------

            private void ListViewItem_ContainerContentChanging(ListViewBase sender, ContainerContentChangingEventArgs args)
            {
                ViewItemCtrl ViewItem = args.ItemContainer.ContentTemplateRoot as ViewItemCtrl;

                ItemBase Item = (ItemBase)args.Item;

             }

    --------------------------------------------------------------------------------------------------------------------------------------

    i want to access ContentTemplateRoot  by item on list view.

    Tuesday, December 30, 2014 1:40 AM

Answers

  • Hi estmob,

    Based on your decription, I know that you want to access Item and ContentTemplateRoot in ContainerContentChanging event, then maybe you can try to do it as following:

    The MainPage.xaml:

     <ListView Name="MyListView" ContainerContentChanging="MyListView_ContainerContentChanging">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <StackPanel>
                            <TextBlock Name="MyTextBlock" Text="{Binding name}"></TextBlock>
                        </StackPanel>
                    </DataTemplate>
                </ListView.ItemTemplate>
     </ListView>


    The MainPage.xaml.cs:

     private void MyListView_ContainerContentChanging(ListViewBase sender, ContainerContentChangingEventArgs args)
            {
                StackPanel ViewItem = args.ItemContainer.ContentTemplateRoot as StackPanel;
                TextBlock Item = (TextBlock)ViewItem.FindName("MyTextBlock");
                Item.Foreground = new SolidColorBrush(Colors.Red);
            }

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Wednesday, December 31, 2014 2:56 AM

All replies

  • You could bind SelectedItem to a property of the viewmodel:

    <ListView ItemsSource="{Binding Things}"
                SelectedItem="{Binding SelectedThing, Mode=TwoWay}" />
    Then you can put your event handling code in the setter.
    Wednesday, December 31, 2014 2:05 AM
  • Hi estmob,

    Based on your decription, I know that you want to access Item and ContentTemplateRoot in ContainerContentChanging event, then maybe you can try to do it as following:

    The MainPage.xaml:

     <ListView Name="MyListView" ContainerContentChanging="MyListView_ContainerContentChanging">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <StackPanel>
                            <TextBlock Name="MyTextBlock" Text="{Binding name}"></TextBlock>
                        </StackPanel>
                    </DataTemplate>
                </ListView.ItemTemplate>
     </ListView>


    The MainPage.xaml.cs:

     private void MyListView_ContainerContentChanging(ListViewBase sender, ContainerContentChangingEventArgs args)
            {
                StackPanel ViewItem = args.ItemContainer.ContentTemplateRoot as StackPanel;
                TextBlock Item = (TextBlock)ViewItem.FindName("MyTextBlock");
                Item.Foreground = new SolidColorBrush(Colors.Red);
            }

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Wednesday, December 31, 2014 2:56 AM