question

Sarah-3412 avatar image
0 Votes"
Sarah-3412 asked Sarah-3412 edited

How to remove selection in ListView by clicking on blank space or any other control in UI?

I am using a ListView and I want to disable the selected item when I click anywhere in the UI or click another control in the UI.

  <ListView x:Name="View" Grid.Row="1" Grid.Column="0" ItemsSource="{Binding View}"  ScrollViewer.HorizontalScrollBarVisibility="Disabled" IsSynchronizedWithCurrentItem="True" SelectedItem="{Binding Contact}" SelectionMode="Single">



windows-wpf
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

PeterFleischer-3316 avatar image
0 Votes"
PeterFleischer-3316 answered Sarah-3412 edited

Hi SYarah,
you can use an attached property in ContactListingViewModel:

     <ListView Grid.Row="1" Grid.Column="0" 
               ItemsSource="{Binding View}"  
               ScrollViewer.HorizontalScrollBarVisibility="Disabled" 
               IsSynchronizedWithCurrentItem="True" 
               SelectedItem="{Binding Contact}"
               local:ContactListingViewModel.AttPropListView="True">
    
 ...
    
     #region attached property
     public static readonly DependencyProperty AttPropListViewProperty =
         DependencyProperty.RegisterAttached("AttPropListView", typeof(bool), typeof(ContactListingViewModel), new PropertyMetadata(false, OnPropChanged));
     public static bool GetAttPropListView(DependencyObject obj) => (bool)obj.GetValue(AttPropListViewProperty);
     public static void SetAttPropListView(DependencyObject obj, bool par) => obj.SetValue(AttPropListViewProperty, par);
     private static void OnPropChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
     {
       ListView lv = d as ListView;
       if (lv == null) return;
       lv.PreviewMouseLeftButtonDown += (s, mbe) =>
       {
         ContactListingViewModel vm = (ContactListingViewModel)(lv.DataContext);
         vm.Contact = null;
         vm.OnPropertyChanged(nameof(Contact));
       };
     }
     #endregion

Result:

182429-x.gif



x.gif (372.7 KiB)
· 8
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi, Please read the question more carefully. It should work for click on blank space in the ListView or on any other control in UI. Your suggestion works only for clicking on ListView. If I click on other control in UI it does not work.

0 Votes 0 ·

Hi Sarah,
which problem you have to use my solution in window? If you click "on other control in UI" click to "edit" or "Delete" reset the selection too! What do you want to do?

![182995-x.gif][1]



0 Votes 0 ·
x.gif (107.2 KiB)
Sarah-3412 avatar image Sarah-3412 PeterFleischer-3316 ·

Hi,When clicking on white spaces (for example under the Delete button) or in the TextBox, your solution does not work.

0 Votes 0 ·
Show more comments