12namespace DataGrid0508.ViewModels
3{4 public class DataGridViewModel:ViewModel
5{6 public DataGridViewModel()7{8 MyList = new ObservableCollection<Person>(910 Enumerable.Range(1,5).Select(i => new Person
11{12 Name ="田中"+ i,13 Age =20+ i,14 AuthMember = i %2==015}));16}171819 private ObservableCollection<Person> _MyList;2021 public ObservableCollection<Person> MyList
22{23 get
24{return _MyList;}25 set
26{27if(_MyList == value)28return;29 _MyList = value;30RaisePropertyChanged();31}32}3334 public voidTestButton()35{36//選択解除37}383940}41}42public class Person
43{44 public string Name { get; set;}45 public int Age { get; set;}46 public bool AuthMember { get; set;}47}
1usingLivet;2usingSystem.Collections.ObjectModel;3usingSystem.Linq;45namespaceQuestions337191.ViewModels6{7publicclassMainWindowViewModel:ViewModel8{9publicObservableCollection<Person> MyList {get;}1011publicMainWindowViewModel()12{13 MyList =newObservableCollection<Person>(14 Enumerable.Range(1,10_000).Select(i =>newPerson15{16 Name ="田中"+ i,17 Age =20+ i,18 AuthMember = i %2==0,19}));20}21}2223publicclassPerson24{25publicstring Name {get;set;}26publicint Age {get;set;}27publicbool AuthMember {get;set;}28}29}
cs
1usingMicrosoft.Xaml.Behaviors;2usingSystem.Linq;3usingSystem.Windows;4usingSystem.Windows.Controls;5usingSystem.Windows.Input;6usingSystem.Windows.Media;78namespaceQuestions337191.Views9{10// [wpf - DataGrid SelectionUnit=Cell disables all support for a selected row? - Stack Overflow](https://stackoverflow.com/questions/9489041/datagrid-selectionunit-cell-disables-all-support-for-a-selected-row)11publicstaticclassDataGridAttachedProperties12{13publicstaticboolGetIsCellSelected(DependencyObject obj)=>(bool)obj.GetValue(IsCellSelectedProperty);14publicstaticvoidSetIsCellSelected(DependencyObject obj,boolvalue)=> obj.SetValue(IsCellSelectedProperty,value);15publicstaticreadonlyDependencyProperty IsCellSelectedProperty
16= DependencyProperty.RegisterAttached("IsCellSelected",typeof(bool),typeof(DataGridAttachedProperties),17newUIPropertyMetadata(false, OnIsCellSelectedChanged));18privatestaticvoidOnIsCellSelectedChanged(DependencyObject d,DependencyPropertyChangedEventArgs e)19{20if(d isDataGridCell cell)21{22var row =FindVisualParent<DataGridRow>(cell);2324// 同じ行内で複数選択がある状態でそのうち1つを選択し直した場合25// そのセルはCellSelectedChangedが出ない(選択状態は変わらない)ので26// 周りの選択解除だけ伝わってしまいバグる27//row.SetValue(IsCellSelectedProperty, e.NewValue);2829// しかたがないので行内全走査30// 重そうだが100桁くらいなら気にならなかった(1000桁あると数秒止まった^^;31var p =FindVisualParent<DataGridCellsPanel>(cell);32var b = p.Children.Cast<DataGridCell>().Any(x => x.IsSelected);33 row.SetValue(IsCellSelectedProperty, b);34}35}3637privatestaticTFindVisualParent<T>(DependencyObject child)whereT:DependencyObject38{39var parentObject = VisualTreeHelper.GetParent(child);40if(parentObject ==null)returnnull;41if(parentObject isT parent)return parent;42elsereturnFindVisualParent<T>(parentObject);43}44}4546publicclassDataGridBehavior:Behavior<DataGrid>47{48publicstaticreadonlyRoutedUICommand UnselectAll =newRoutedUICommand("選択解除","UnselectAll",typeof(DataGridBehavior));4950protectedoverridevoidOnAttached()51{52base.OnAttached();53var commandBinding =newCommandBinding(UnselectAll, Executed);54 AssociatedObject.CommandBindings.Add(commandBinding);55}5657protectedoverridevoidOnDetaching()58{59base.OnDetaching();60 AssociatedObject.CommandBindings.Clear();61}6263privatevoidExecuted(object target,ExecutedRoutedEventArgs e)64{65if(AssociatedObject.SelectionUnit == DataGridSelectionUnit.FullRow)66 AssociatedObject.UnselectAll();67else68 AssociatedObject.UnselectAllCells();69 e.Handled =true;70}71}72}
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/05/10 14:12
2021/05/10 15:01
2021/05/12 11:24
2021/05/12 11:30
2021/05/12 14:44
2021/05/12 15:03
2021/05/12 15:28