SelectedItems for ListView
SelectedItems not exist for LisView you can use this code for adding this property and using binding
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
#region SelectedItems (ATTACHED) [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:Valider les arguments de méthodes publiques", MessageId = "0")] public static IList GetSelectedItems(DependencyObject obj) { return (IList)obj.GetValue(SelectedItemsProperty); } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:Valider les arguments de méthodes publiques", MessageId = "0")] public static void SetSelectedItems(DependencyObject obj, IList value) { obj.SetValue(SelectedItemsProperty, value); } // Using a DependencyProperty as the backing store for SelectedItems. This enables animation, styling, binding, etc... public static readonly DependencyProperty SelectedItemsProperty = DependencyProperty.RegisterAttached( "SelectedItems", typeof(IList), typeof(ListViewHelper), // new PropertyMetadata(new object()) // new FrameworkPropertyMetadata(new object(), new PropertyChangedCallback(SelectedItemsChanged)) new FrameworkPropertyMetadata(new List<object>(), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, new PropertyChangedCallback(SelectedItemsChanged)) ); private static void SelectedItemsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { ListView control = d as ListView; if (control != null) { if (!GetIsConnected(control)) { control.SelectionChanged += _SelectionChanged; SetIsConnected(control, true); } } } private static void _SelectionChanged(object sender, SelectionChangedEventArgs e) { ListView list = sender as ListView; if (list != null) { //Get list box's selected items. IEnumerable listBoxSelectedItems = list.SelectedItems; SetSelectedItems(list, listBoxSelectedItems as IList); } } #endregion #region IsConnected (ATTACHED) [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:Valider les arguments de méthodes publiques", MessageId = "0")] public static bool GetIsConnected(DependencyObject obj) { return (bool)obj.GetValue(IsConnectedProperty); } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:Valider les arguments de méthodes publiques", MessageId = "0")] public static void SetIsConnected(DependencyObject obj, bool value) { obj.SetValue(IsConnectedProperty, value); } // Using a DependencyProperty as the backing store for IsConnected. This enables animation, styling, binding, etc... public static readonly DependencyProperty IsConnectedProperty = DependencyProperty.RegisterAttached( "IsConnected", typeof(bool), typeof(ListViewHelper), new PropertyMetadata(false) ); #endregion |
and usage like this
1 2 3 4 5 |
<ListView ItemsSource="{Binding Contactes}" Margin="0 4 0 0" BorderBrush="red" BorderThickness="1" local:ListViewHelper.SelectedItems="{Binding SelectedItems}"/> |
Categories: Non classé
Commentaires récents