access gridview selection within listbox
-
lundi 20 août 2012 21:35
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>
- Modifié Cubangt lundi 20 août 2012 21:35
Toutes les réponses
-
lundi 20 août 2012 23:14Modérateur
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
- Modifié XAML guyMicrosoft Community Contributor, Moderator lundi 20 août 2012 23:16 tidied code
- Marqué comme réponse Kee PoppyModerator jeudi 6 septembre 2012 03:50

