Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Diagnostics;
- using System.Linq;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Input;
- using Telerik.Windows.Controls;
- using System;
- namespace App.UI.Behaviors
- {
- public class ItemSelectionChangedBehavior
- {
- #region Command Dependency Property
- public static readonly DependencyProperty CommandProperty = DependencyProperty.RegisterAttached(
- "Command",
- typeof(ICommand),
- typeof(ItemSelectionChangedBehavior),
- new UIPropertyMetadata(null, OnCommandChanged));
- public static ICommand GetCommand(DependencyObject obj)
- {
- return (ICommand)obj.GetValue(CommandProperty);
- }
- public static void SetCommand(DependencyObject obj, ICommand value)
- {
- obj.SetValue(CommandProperty, value);
- }
- static void OnCommandChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
- {
- if (args.OldValue != null)
- {
- if (sender is RadListBox)
- {
- var lstBox = sender as RadListBox;
- if (lstBox != null)
- {
- lstBox.SelectionChanged -= ListBox_SelectionChanged;
- }
- }
- else if (sender is RadGridView)
- {
- var gview = sender as RadGridView;
- if (gview != null)
- {
- gview.SelectionChanged -= GridView_SelectionChanged;
- }
- }
- }
- if (args.NewValue != null)
- {
- if (sender is RadListBox)
- {
- var lstBox = sender as RadListBox;
- if (lstBox != null)
- {
- lstBox.SelectionChanged += ListBox_SelectionChanged;
- }
- }
- else if (sender is RadGridView)
- {
- var gview = sender as RadGridView;
- if (gview != null)
- {
- gview.SelectionChanged += GridView_SelectionChanged;
- }
- }
- }
- }
- #endregion
- static void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- var listBox = sender as RadListBox;
- if (listBox == null)
- return;
- var selectedFiles = (IList)listBox.SelectedItems;
- var command = GetCommand(listBox);
- if (command != null)
- {
- if (command.CanExecute(selectedFiles))
- {
- command.Execute(selectedFiles);
- }
- }
- Debug.WriteLineIf(false, string.Format("ItemSelectionChangedBehavior -> ListBox.SelectedItems.Count: {0}", SelectedFiles.Count));
- }
- static void GridView_SelectionChanged(object sender, SelectionChangeEventArgs e)
- {
- var gridView = sender as RadGridView;
- if (gridView == null)
- return;
- var selectedFiles = gridView.SelectedItems.ToList();
- var command = GetCommand(gridView);
- if (command != null)
- {
- if (command.CanExecute(selectedFiles))
- {
- command.Execute(selectedFiles);
- }
- }
- Debug.WriteLineIf(false, string.Format("ItemSelectionChangedBehavior -> Grid.SelectedItems.Count: {0}", selectedFiles.Count));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment