locked
Having trouble getting sellectedvalue from ListView using templates (c# Windows store app)

    Question

  • I am trying to get the values of the selected ListView item but am not able to get any of the values.

    xaml is...

          

    <StackPanelx:Name="StackPanel1"Grid.Column="1"HorizontalAlignment="Left"Height="587"Margin="21,10,0,0"Grid.Row="1"VerticalAlignment="Top"Width="524">

    <ListViewName="ListView2"HorizontalAlignment="Left"Height="530"Margin="10,10,0,0"VerticalAlignment="Top"Width="504"SelectionChanged="ListView2_SelectionChanged"ItemClick="ListView2_ItemClick" >

    <ListView.ItemTemplate>

    <DataTemplate>

    <GridHeight="110"Margin="6">

    <Grid.ColumnDefinitions>

    <ColumnDefinitionWidth="Auto"/>

    <ColumnDefinitionWidth="*"/>

    </Grid.ColumnDefinitions>

    <StackPanelGrid.Column="1"VerticalAlignment="Top"Margin="10,0,0,0">

    <StackPanelOrientation="Horizontal">

    <TextBlockFontWeight="Bold"  Text="Tag/Item:"TextWrapping="NoWrap"/>

    <TextBlockFontWeight="Bold"  Text="{Bindingtag}"TextWrapping="NoWrap"/>

    <TextBlockFontWeight="Bold"  Text=" / "TextWrapping="NoWrap"Padding="3,0,0,0"/>

    <TextBlockFontWeight="Bold"Text="{Bindingitem}" />

    </StackPanel>

    <TextBlockText="{Bindingdescription}" />

    <StackPanelOrientation="Horizontal">

    <TextBlockText="Case: "TextWrapping="NoWrap"/>

    <TextBlockText="{Bindingcaseno}" />

    </StackPanel>

    <TextBlockText="{Bindingstor}" />

    <TextBlockText="=========================" />

    </StackPanel>

    </Grid>

    </DataTemplate>

    </ListView.ItemTemplate>

    </ListView>

    </StackPanel>

    No matter what I try in my ListView2_SelectionChanged event, I cannot get any of the clicked values. 

    Any ideas?

    Thanks.

    Monday, August 4, 2014 4:25 PM

Answers

  • Hi DaleFurseth,

    I used your code to build a project for test, it worked on my side. Please see the code fragment.

    XAML:

    <StackPanel Background="Wheat" x:Name="StackPanel1" Grid.Column="1" HorizontalAlignment="Left" Height="587" Margin="21,10,0,0" Grid.Row="1" VerticalAlignment="Top" Width="524">
    
    
            <ListView Name="ListView2" HorizontalAlignment="Left" Height="530" Margin="10,10,0,0" VerticalAlignment="Top"
    
                      Width="504" SelectionChanged="ListView2_SelectionChanged">
    
    
                <ListView.ItemTemplate>
    
    
                    <DataTemplate>
    
    
                        <Grid Height="110" Margin="6">
    
    
                            <Grid.ColumnDefinitions>
    
    
                                <ColumnDefinition Width="Auto"/>
    
    
                                <ColumnDefinition Width="*"/>
    
    
                            </Grid.ColumnDefinitions>
    
    
                            <StackPanel Grid.Column="1" VerticalAlignment="Top" Margin="10,0,0,0">
    
    
                                <StackPanel Orientation="Horizontal">
    
    
                                    <TextBlock FontWeight="Bold"  Text="Tag/Item:" TextWrapping="NoWrap"/>
    
    
                                    <TextBlock FontWeight="Bold"  Text="{Binding tag}" TextWrapping="NoWrap"/>
    
    
                                    <TextBlock FontWeight="Bold"  Text=" / " TextWrapping="NoWrap" Padding="3,0,0,0"/>
    
    
                                    <TextBlock FontWeight="Bold" Text="{Binding item}" />
    
    
                                    </StackPanel>
    
    
                                    <TextBlock Text="{Binding description}" />
    
    
                                    <StackPanel Orientation="Horizontal">
    
    
                                        <TextBlock Text="Case: " TextWrapping="NoWrap"/>
    
    
                                        <TextBlock Text="{Binding caseno}" />
    
    
                                        </StackPanel>
    
    
                                        <TextBlock Text="{Binding stor}" />
    
    
                                        <TextBlock Text="=========================" />
    
    
                                        </StackPanel>
    
    
                           </Grid>
    
    
                    </DataTemplate>
    
    
                </ListView.ItemTemplate>
    
    
                </ListView>
    
    
            <TextBlock x:Name="MSG" Foreground="LightBlue" FontSize="30"></TextBlock>
    
       </StackPanel>
    

    C# Code:

    public sealed partial class MainPage : Page
    
        {
    
            private List<ItemDetails> list = new List<ItemDetails>();
    
    
            public MainPage()
    
            {
    
                this.InitializeComponent();
    
            }
    
    
            private void ListView2_SelectionChanged(object sender, SelectionChangedEventArgs e)
    
            {
    
                var item = ListView2.SelectedItem as ItemDetails;
    
                MSG.Text = item.ToString();
    
            }
    
    
            private void Page_Loaded(object sender, RoutedEventArgs e)
    
            {
    
                list.AddRange(new ItemDetails[]{
    
                    new ItemDetails(){ tag="aa", caseno="a1", description="aaaa", item="a11", stor="a0000"} ,
    
                     new ItemDetails(){ tag="bb", caseno="a1", description="aaaa", item="b22", stor="a0000"} ,
    
                      new ItemDetails(){ tag="cc", caseno="a1", description="aaaa", item="c33", stor="a0000"} ,
    
                       new ItemDetails(){ tag="dd", caseno="a1", description="aaaa", item="d44", stor="a0000"} ,
    
                        new ItemDetails(){ tag="ee", caseno="a1", description="aaaa", item="e55", stor="a0000"} 
    
                    });
    
    
                ListView2.ItemsSource =  list;
    
            }
    
        }
    
    
        public class ItemDetails
    
        {
    
            public string tag { get; set; }
    
            public string item { get; set; }
    
            public string description { get; set; }
    
            public string caseno { get; set; }
    
            public string stor { get; set; }
    
    
            public override string ToString()
    
            {
    
                return string.Format("tag/item: {0}/{1}", tag, item);
    
            }
    
    }
    

    Regards,


    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. &lt;br/&gt; Click &lt;a href=&quot;http://support.microsoft.com/common/survey.aspx?showpage=1&amp;scid=sw%3Ben%3B3559&amp;theme=tech&quot;&gt; HERE&lt;/a&gt; to participate the survey.

    Tuesday, August 5, 2014 1:38 AM
    Moderator