Reputation: 1167
I would like to retrieve all data from clicked ListViews row. Also to hide that id column if it's possible. I tried to use commands with click event and pass that id field into the commandParammeter but no success.
Also searched for other approaches but nothing. Just for information, I need this functionality to show detailed information about each clicked user into other listView so this data would be much easier to view for other users.
Conclusion: Need to get clicked row data and if it's possible, also hide that id column.
Xaml code:
<ListView x:Name="lstUsers" ItemsSource="{Binding UserList,UpdateSourceTrigger=PropertyChanged}">
<ListView.View>
<GridView x:Name="grdUsers">
<GridViewColumn Header="Hidden_Id" DisplayMemberBinding="{Binding Id}"/>
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}"/>
<GridViewColumn Header="Surname" DisplayMemberBinding="{Binding Surname}"/>
<GridViewColumn Header="Age" DisplayMemberBinding="{Binding Age}"/>
</GridView>
</ListView.View>
</ListView>
And inside my ViewModel is placed a UserList.Any kind of elegant approach will do that gets the clicked row data.
I looked into the link that @BradleyDotNET gave and the code now looks like this.
<ListView x:Name="lBoxUser" ItemsSource="{Binding UserList, UpdateSourceTrigger=PropertyChanged}" Width="300"
IsSynchronizedWithCurrentItem="True"
Visibility="Visible" SelectionMode="Single"
SelectedValuePath="Id"
SelectionChanged="lBoxUser_SelectionChanged">
//this part hasn't changed
</ListView>
SelectedValuePath: attribute that will bind one of the userList object. In my case it is 'Id'.
SelectionChanged: just adding event handler that will execute on selection changed event and get all selected row data. I know it would be more elegant to user MVVM pattern but Commands somehow didn't work for me.
Also to display that 'SelectedValue'.
<TextBlock Text="{Binding SelectedValue, ElementName=lBoxUser}"/>
I found much easier way to do it.
<ListView.SelectedItem>
<Binding Path="UserProperty" UpdateSourceTrigger="PropertyChanged"/>
</ListView.SelectedItem>
Just added User class property inside my ViewModel and thats all. It gets all selected user data. Also now it counts as MVVM pattern.
Upvotes: 2
Views: 884
Reputation: 61349
You are well on your way!
To get rid of the "ID" column, its pretty easy; just delete it!
<ListView x:Name="lstUsers" ItemsSource="{Binding UserList,UpdateSourceTrigger=PropertyChanged}">
<ListView.View>
<GridView x:Name="grdUsers">
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}"/>
<GridViewColumn Header="Surname" DisplayMemberBinding="{Binding Surname}"/>
<GridViewColumn Header="Age" DisplayMemberBinding="{Binding Age}"/>
</GridView>
</ListView.View>
</ListView>
To get the information for the currently selected item, look at the SelectedValue
(MSDN) property of ListView
it will hold the bound object of the currently selected item.
Upvotes: 1