none
get selected value for a listview

    Question

  • i have a listview that i bind multiple columns to and i set the SelectedValuePath = "sResID" and i can get that value with no problems. But i also want to get the value of the selected resident ____ in the listview. here is the code to create my listview. any help i really appreciate. thanks

    <ListView Name="lstResidents" Grid.Column="0" Grid.Row="0" Grid.RowSpan="3"  Grid.ColumnSpan="1" ItemsSource="{Binding}" 
                          BorderBrush="Transparent" SelectionMode="Single" Background="Transparent" Foreground="White" FontSize="39" ItemContainerStyle="{StaticResource ItemContStyle}" 
                          Margin="0,0,0,40" SelectionChanged="lstResidents_SelectionChanged" PreviewMouseDown="lstResidents_PreviewMouseDown"
                    <ListView.Resources> 
                        <LinearGradientBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" StartPoint="0,0" EndPoint="1,1"
                                <GradientStop Color="#86BCE8" Offset="0.0"/> 
                                <GradientStop Color="#5DA5DF" Offset="0.3"/> 
                                <GradientStop Color="#86BCE8" Offset="0.6"/> 
                            </LinearGradientBrush> 
                        <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="#86BCE8" /> 
                    </ListView.Resources> 
                    <ListView.View> 
                        <GridView> 
                            <GridViewColumn Width="50" Header="Icon" CellTemplate="{StaticResource MaleorFemaleIcon}"></GridViewColumn>
                            <GridViewColumn Width="0" Header="sSex" DisplayMemberBinding="{Binding Path=sSex}"/>

                            <GridViewColumn Width="0" Header="sResID" DisplayMemberBinding="{Binding Path=sResID}"/> 
                            <GridViewColumn Width="600" Header="Name" DisplayMemberBinding="{Binding Path=Name}"/> 
                            <GridViewColumn Width="0" Header="iStatus" DisplayMemberBinding="{Binding Path=iStatus}"/> 
                            <GridView.ColumnHeaderContainerStyle> 
                                <Style TargetType="GridViewColumnHeader"
                                    <Setter Property="Visibility" Value="Collapsed"></Setter> 
                                </Style> 
                            </GridView.ColumnHeaderContainerStyle> 
                        </GridView> 
                    </ListView.View> 
                </ListView> 
     
    • Edited by cbreidenstein Tuesday, September 02, 2008 4:29 PM fix code
    Tuesday, September 02, 2008 4:20 PM

Answers

  • Remove setting the SelectedValuePath, then you will get the complete object instead of a single property.
    • Marked as answer by cbreidenstein Tuesday, September 02, 2008 8:01 PM
    Tuesday, September 02, 2008 7:20 PM

All replies

  • Can you give some more clarification regarding what you want to do? Why do you need to set a SelectedValuePath? Do you need to select the listviewitem based on some data object?
    Tuesday, September 02, 2008 6:10 PM
  • i set the SelectedValuePath in order to reference the sResID column when i called the lstResidents.SelectedValue on the MouseUp event on the listview. Which that works fine, how i want it to. But I also want to be able to get the selectedvalue for other columns in the ListView. So in my code after the user click a row in the listview, i want to get the sResID column value (which is already working fine) but i also need to get the sSex value of the clicked column. thanks
    Tuesday, September 02, 2008 6:15 PM
  • Remove setting the SelectedValuePath, then you will get the complete object instead of a single property.
    • Marked as answer by cbreidenstein Tuesday, September 02, 2008 8:01 PM
    Tuesday, September 02, 2008 7:20 PM
  • You can also do this:

     
    (lstResidents.Items[lstResidents.SelectedIndex] as yourItemType).sSex;  
     


    Dr. WPF - Online Office at http://drwpf.com/blog/
    Tuesday, September 02, 2008 7:55 PM
  • i removed the selectedValuePath and used what i have below and it works. It seems a little wierd to me here but it works i guess, so thank you very much for your help.

    Dr.WPF, I'm not really sure what you mean with your suggestion, i think it may be a little over my head or I'm just not seeing it... I'm not really sure what you mean with yourItemType. I don't have a resident class setup with properties for sSex,resident ID and so on, if that is what you meant with that. but please feel free to ellaborate, I am definitly open to learning more. thanks both of you



    Dim dr As System.Data.DataRowView 
    dr = lstResidents.SelectedValue 
    sSex = Convert.ToString(dr.Row.ItemArray(5))
    • Edited by cbreidenstein Tuesday, September 02, 2008 8:10 PM edit
    Tuesday, September 02, 2008 8:08 PM
  • You are binding to a data table, so in your case you could do something like this:

     
    (lstResidents.Items[lstResidents.SelectedIndex] as DataRowView).Row["sSex"];  

    or you could use the array index, as you're doing above, for slightly better perf.  But removing SelectedValuePath is just as good.
    Dr. WPF - Online Office at http://drwpf.com/blog/
    • Proposed as answer by Jim Zhou - MSFT Tuesday, September 09, 2008 1:48 AM
    Tuesday, September 02, 2008 8:31 PM