access gridview selection within listbox
-
Monday, August 20, 2012 9:35 PM
I have the following setup on a page within our WPF app. But cant get the selected gridview item. How can i do that with the following setup? I need the value in the # column
<ListView Name="lbCase" ItemsSource="{Binding}" SelectionChanged="lbCase_SelectionChanged" SelectionMode="Single"> <ListView.View> <GridView > <GridViewColumn DisplayMemberBinding="{Binding Path=number}" > <GridViewColumn.Header> <TextBlock Text="#" /> </GridViewColumn.Header> </GridViewColumn> <GridViewColumn DisplayMemberBinding="{Binding Path=name}" > <GridViewColumn.Header> <TextBlock Text="Name" /> </GridViewColumn.Header> </GridViewColumn> </GridView> </ListView.View> </ListView>
- Edited by Cubangt Monday, August 20, 2012 9:35 PM
All Replies
-
Monday, August 20, 2012 11:14 PMModerator
Can you use the SelectedItem property?
lbCase.SelectedItem?
In the selectionChanged event it is:
private void lbCase_SelectionChanged(object sender, SelectionChangedEventArgs e) { var lb = sender as ListBox; var current = lb.SelectedItem; }Or you could change your bindings and bind the SelectedItem property to a property in code:
<Window x:Class="WpfApplication66.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" > <ListView Name="lbCase" ItemsSource="{Binding MyCollection}" SelectedItem="{Binding CurrentItem}" SelectionMode="Single"> <ListView.View> <GridView > <GridViewColumn DisplayMemberBinding="{Binding Path=number}" > <GridViewColumn.Header> <TextBlock Text="#" /> </GridViewColumn.Header> </GridViewColumn> <GridViewColumn DisplayMemberBinding="{Binding Path=name}" > <GridViewColumn.Header> <TextBlock Text="Name" /> </GridViewColumn.Header> </GridViewColumn> </GridView> </ListView.View> </ListView> </Window>using System.Collections.Generic; using System.Windows; using System.Windows.Controls; namespace WpfApplication66 { public partial class MainWindow : Window { public List<MyClass> MyCollection { get; set; } public MyClass CurrentItem { get; set; } public MainWindow() { InitializeComponent(); MyCollection = new List<MyClass> { ... }; DataContext = this; } } }
Regards,
Pete
#PEJL
- Edited by XAML guyMicrosoft Community Contributor, Moderator Monday, August 20, 2012 11:16 PM tidied code
- Marked As Answer by Kee PoppyModerator Thursday, September 06, 2012 3:50 AM

