none
Getting WPF ListView Cell Content

    Question

  • Hi,

     

    I have a ListView that consists of single column horizontally oriented.

    The cells contains textbox bount to a list.

    How do I traverse the cells to access each textbox?

    Sunday, October 24, 2010 10:09 AM

Answers

  • Hi NewToPowerShell,

    You can follow the steps listed below to retrive any elements in your DataTemplate in a ListView.

    1. Use ListView.ItemContainerGenerator property to get its ItemContainerGenerator,
    2. Call ItemContainerGenerator.ContainerFromItem method to get the ListViewItem that contains this item.
    3. Use VisualTreeHelper class to look into the visual tree and get the ContentPresenter in this ListViewItem.
    4. Call FrameworkTemplate.FindName method on ContentPresenter's ContentTemplate to get the element you want.

    Please refer to http://msdn.microsoft.com/en-us/library/system.windows.frameworktemplate.findname.aspx for more infomation. There is an excellent sample in this link.

    Hope this helps.

    If you still have any doubts or concerns about this issue, please feel free to let me know.

    Best regards,

    Min


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    Thursday, October 28, 2010 2:09 AM
    Moderator

All replies

  • Here is the answer in code:

    <Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
      <Grid>
        <ListView x:Name="myLV">
          <ListView.Items>
            <TextBox></TextBox>
            <TextBox></TextBox>
            <TextBox></TextBox>
          </ListView.Items>
        </ListView>
      </Grid>
    </Window>
    

    The C# file to traverse the cells:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    
    namespace WpfApplication1
    {
      /// <summary>
      /// Interaction logic for MainWindow.xaml
      /// </summary>
      public partial class MainWindow : Window
      {
        public MainWindow()
        {
          InitializeComponent();
        }
    
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
          for (int j = 0; j < myLV.Items.Count; j++)
          {
          
            TextBox lstViewItem = myLV.Items[j] as TextBox;
            lstViewItem.Text = "This is Item "+j;
          } 
    
        }
      }
    }
    
    

    -
    Sunday, October 24, 2010 10:44 AM
  • Here is my XAML, where MeasureBox inherits from TextBox.

    How to get MeasureBox, knowing that I am binding to SeriesValues list that contains SeriesValue object. (SeriesValue Object is simple object containing integer properties)

    so, in my case myLV.Items[j] returns the obect not the textbox

    <ListView Name="SeriesLV" ItemsSource="{Binding}" Height="30">
          <ListView.ItemsPanel>
            <ItemsPanelTemplate>
              <StackPanel Orientation="Horizontal"></StackPanel>
            </ItemsPanelTemplate>
          </ListView.ItemsPanel>
          <ListView.ItemTemplate>
            <DataTemplate>
              <my:MeasureBox x:Name="measure" Text="{Binding Path=Number}" MinWidth="30" MinHeight="30" MaxLength="2" IsEnabled="{Binding Path=EnableMeasure}" TextChanged="MeasureTextBox_TextChanged" Index="{Binding Path=idx}"></my:MeasureBox>
            </DataTemplate>
          </ListView.ItemTemplate>
        </ListView>
    Sunday, October 24, 2010 12:14 PM
  • Sunday, October 24, 2010 4:41 PM
  • Hi NewToPowerShell,

    You can follow the steps listed below to retrive any elements in your DataTemplate in a ListView.

    1. Use ListView.ItemContainerGenerator property to get its ItemContainerGenerator,
    2. Call ItemContainerGenerator.ContainerFromItem method to get the ListViewItem that contains this item.
    3. Use VisualTreeHelper class to look into the visual tree and get the ContentPresenter in this ListViewItem.
    4. Call FrameworkTemplate.FindName method on ContentPresenter's ContentTemplate to get the element you want.

    Please refer to http://msdn.microsoft.com/en-us/library/system.windows.frameworktemplate.findname.aspx for more infomation. There is an excellent sample in this link.

    Hope this helps.

    If you still have any doubts or concerns about this issue, please feel free to let me know.

    Best regards,

    Min


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    Thursday, October 28, 2010 2:09 AM
    Moderator