Tutorial :WPF ListView with “readonly” SelectedItems



Question:

Here's my situation: I've a many-to-many relationship I'm trying to model in a WPF app, while editing one side of the relationship. The situation is I've a set of users that belong to zero to many groups. While editing a user, I use a ListView with SelectionMode set to Many. Groups that the user belongs to are marked as selected items in the list view.

My problem is this: Not all users are entitled to modify a user's group memberships. But, they should be able to view them. So far, not really an issue, because there are, of course, entitlement checks that kick out when the changes are saved. What I'd like to do, however, is effectively disable the ListView (at least it's ability to modify the SelectedItems), while still maintaining other basic functionality (scrolling, especially, I've also implemented sorting when headers are clicked). This way a user can't inadvertently make changes they're not allowed to and then encounter an error later, when they go to save the changes.

Currently, I'm just disabling the ListView entirely, if the user does not have sufficient entitlements. But, as I mentioned, it effectively disables all other functionality (scrolling, etc), and thus is suboptimal from a user experience perspective. The number of items in the list (currently several hundred) necessitates that scrolling is necessary to see all items.

I've tried intercepting various Mouse events (disregarding alternate input access for the time being), and marking the events as handled when the user clicked on a ListViewItem and marking the event as handled, but the ListView still modified the selection.

I've though of just handling the SelectionChanged event and just restoring the previous state, but this seems a little bit overkill (but, it might be the simplest -read easiest- solution).

Anyone have any thoughts on how to accomplish this behavior?


Solution:1

You could disable the ListViewItems but leave the ListView itself enabled. That way the scrollbars would work, but it would be clear to the user that they cannot interact with the items in the list. You can disable the items using an ItemContainerStyle like this:

<ListView.ItemContainerStyle>      <Style TargetType="ListViewItem">          <Setter Property="IsEnabled" Value="False"/>      </Style>  </ListView.ItemContainerStyle>  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »