locked
ListView Multiple Selected Items Values

    Question

  • I have a listview call ASSETSLIST. It is a multiple selection mode list.

    How can I get the values of one of the textblocks that make up that listview if multiple are selected.

    I want those values when a button is clicked.

    I've been trying to use a foreach statement without success.

    Wednesday, February 19, 2014 6:22 PM

Answers

  • Hi,

    which property do you bind to TextBlock and what DataTemplate do you descirbe in your ListView. You should post your codes in forum. And also there another method you can refer to,you can get TextBlock Control in ListView DataTemplate and get its Text:

     private void Button_Click(object sender, RoutedEventArgs e)
            {
                var items = listview.SelectedItems;
                
                
                foreach (var item in items)
                {
                    var testitem = item as Test;
    
                    var listviewitem = listview.ItemContainerGenerator.ContainerFromItem(testitem) as ListViewItem;
                    
                    var childTextBlock = FindChild<TextBlock>(listviewitem, "title");
                    string text = childTextBlock.Text.ToString();//get each TextBlock Text
                }
    
            }
            public static T FindChild<T>(DependencyObject parent, string childName) where T : DependencyObject
            {
                if (parent == null) return null;
                T foundChild = null;
                int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
    
                for (int i = 0; i < childrenCount; i++)
                {
                    var child = VisualTreeHelper.GetChild(parent, i);
                    T childType = child as T;
    
                    if (childType == null)
                    {
                        foundChild = FindChild<T>(child, childName);
                        if (foundChild != null) break;
                    }
                    else if (!string.IsNullOrEmpty(childName))
                    {
                        var frameworkElement = child as FrameworkElement;
                        if (frameworkElement != null && frameworkElement.Name == childName)
                        {
                            foundChild = (T)child;
                            break;
                        }
    
                        foundChild = FindChild<T>(child, childName);
                        if (foundChild != null) break;
                    }
                    else
                    {
                        foundChild = (T)child;
                        break;
                    }
                }
    
                return foundChild;
            }
            
    Best Wishes!


    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. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Monday, February 24, 2014 9:56 AM

All replies

  • Hi,

    There is a simple sample you can refer to:

    In XAML:

     <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"></RowDefinition>
                <RowDefinition Height="Auto"></RowDefinition>
            </Grid.RowDefinitions>
            <ListView x:Name="listview" ItemsSource="{Binding }" SelectionMode="Multiple" Grid.Row="0" >
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <StackPanel>
                            <TextBlock x:Name="title" Text="{Binding Title}" />
                        </StackPanel>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
            <Button Content="Button" HorizontalAlignment="Left"  VerticalAlignment="Top" Height="44" Grid.Row="1" Click="Button_Click"/>
        </Grid>

    code-behind:

     public sealed partial class MainPage : Page
        {
            public MainPage()
            {
                this.InitializeComponent();
                List<Test> test = new List<Test>();
                test.Add(new Test { Title = "Items1" });
                test.Add(new Test { Title = "Items2" });
                test.Add(new Test { Title = "Items3" });
                test.Add(new Test { Title = "Items4" });
                listview.DataContext = test;
    
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                var items = listview.SelectedItems;
                foreach(var item in items)
                {
                    var testitem = item as Test;
                    string text = testitem.Title.ToString();//get each TextBlock Text
                }
    
            }
        }
        public class Test
        {
            public string Title { get; set; }
        }
    Best Wishes!


    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. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Thursday, February 20, 2014 3:20 AM
  • That gets me close, but what if I am populating the textblocks from a WCF service like so.

    ObservableCollection<ServiceReference1.ALLASSETIDS> list = awaitmyservice.GetAssetIDNameOnlyAsync();

    Thursday, February 20, 2014 12:41 PM
  • Hi,

    which property do you bind to TextBlock and what DataTemplate do you descirbe in your ListView. You should post your codes in forum. And also there another method you can refer to,you can get TextBlock Control in ListView DataTemplate and get its Text:

     private void Button_Click(object sender, RoutedEventArgs e)
            {
                var items = listview.SelectedItems;
                
                
                foreach (var item in items)
                {
                    var testitem = item as Test;
    
                    var listviewitem = listview.ItemContainerGenerator.ContainerFromItem(testitem) as ListViewItem;
                    
                    var childTextBlock = FindChild<TextBlock>(listviewitem, "title");
                    string text = childTextBlock.Text.ToString();//get each TextBlock Text
                }
    
            }
            public static T FindChild<T>(DependencyObject parent, string childName) where T : DependencyObject
            {
                if (parent == null) return null;
                T foundChild = null;
                int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
    
                for (int i = 0; i < childrenCount; i++)
                {
                    var child = VisualTreeHelper.GetChild(parent, i);
                    T childType = child as T;
    
                    if (childType == null)
                    {
                        foundChild = FindChild<T>(child, childName);
                        if (foundChild != null) break;
                    }
                    else if (!string.IsNullOrEmpty(childName))
                    {
                        var frameworkElement = child as FrameworkElement;
                        if (frameworkElement != null && frameworkElement.Name == childName)
                        {
                            foundChild = (T)child;
                            break;
                        }
    
                        foundChild = FindChild<T>(child, childName);
                        if (foundChild != null) break;
                    }
                    else
                    {
                        foundChild = (T)child;
                        break;
                    }
                }
    
                return foundChild;
            }
            
    Best Wishes!


    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. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Monday, February 24, 2014 9:56 AM