|
|
|
@ -0,0 +1,102 @@ |
|
|
|
|
using DataGridEventTriggerSample.Model; |
|
|
|
|
using Microsoft.Xaml.Behaviors; |
|
|
|
|
using System; |
|
|
|
|
using System.Collections.Generic; |
|
|
|
|
using System.Collections.ObjectModel; |
|
|
|
|
using System.ComponentModel; |
|
|
|
|
using System.Linq; |
|
|
|
|
using System.Text; |
|
|
|
|
using System.Threading.Tasks; |
|
|
|
|
using System.Windows; |
|
|
|
|
using System.Windows.Controls; |
|
|
|
|
using System.Windows.Input; |
|
|
|
|
using System.Windows.Media; |
|
|
|
|
|
|
|
|
|
namespace DataGridEventTriggerSample.ViewModel |
|
|
|
|
{ |
|
|
|
|
internal class StudentViewModel : Behavior<DataGrid>, INotifyPropertyChanged |
|
|
|
|
{ |
|
|
|
|
public static readonly DependencyProperty MouseDownProperty = DependencyProperty.Register(nameof(MouseDownCommand), typeof(ICommand), typeof(StudentViewModel), new PropertyMetadata(null)); |
|
|
|
|
|
|
|
|
|
private ObservableCollection<Student> _students; |
|
|
|
|
|
|
|
|
|
public event PropertyChangedEventHandler? PropertyChanged; |
|
|
|
|
|
|
|
|
|
public StudentViewModel() |
|
|
|
|
{ |
|
|
|
|
this.Students.Add(new Student { Name = "Agent1", Age = 20, Address = "Location1" }); |
|
|
|
|
this.Students.Add(new Student { Name = "Agent2", Age = 21, Address = "Location2" }); |
|
|
|
|
this.Students.Add(new Student { Name = "Agent3", Age = 22, Address = "Location3" }); |
|
|
|
|
this.Students.Add(new Student { Name = "Agent4", Age = 23, Address = "Location4" }); |
|
|
|
|
this.Students.Add(new Student { Name = "Agent5", Age = 24, Address = "Location5" }); |
|
|
|
|
this.Students.Add(new Student { Name = "Agent6", Age = 25, Address = "Location6" }); |
|
|
|
|
this.Students.Add(new Student { Name = "Agent7", Age = 26, Address = "Location7" }); |
|
|
|
|
this.Students.Add(new Student { Name = "Agent8", Age = 27, Address = "Location8" }); |
|
|
|
|
this.Students.Add(new Student { Name = "Agent9", Age = 28, Address = "Location9" }); |
|
|
|
|
this.Students.Add(new Student { Name = "Agent10", Age = 29, Address = "Location10" }); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public ObservableCollection<Student> Students |
|
|
|
|
{ |
|
|
|
|
get |
|
|
|
|
{ |
|
|
|
|
if (_students == null) |
|
|
|
|
_students = new ObservableCollection<Student>(); |
|
|
|
|
|
|
|
|
|
return _students; |
|
|
|
|
} |
|
|
|
|
set |
|
|
|
|
{ |
|
|
|
|
_students = value; |
|
|
|
|
OnPropertyChanged("Students"); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public ICommand MouseDownCommand |
|
|
|
|
{ |
|
|
|
|
get { return (ICommand)GetValue(MouseDownProperty); } |
|
|
|
|
set { SetValue(MouseDownProperty, value); } |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
protected virtual void OnPropertyChanged(string propertyName) |
|
|
|
|
{ |
|
|
|
|
if (PropertyChanged != null) |
|
|
|
|
PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void OnDataGridMouseDown(object sender, MouseEventArgs e) |
|
|
|
|
{ |
|
|
|
|
DataGrid? dataGrid = sender as DataGrid; |
|
|
|
|
if (dataGrid == null) |
|
|
|
|
return; |
|
|
|
|
|
|
|
|
|
Student? student = dataGrid.SelectedItem as Student; |
|
|
|
|
if (student == null) |
|
|
|
|
return; |
|
|
|
|
|
|
|
|
|
for (int i = 0; i < this.Students.Count; i++) |
|
|
|
|
{ |
|
|
|
|
DataGridRow? row = dataGrid.ItemContainerGenerator.ContainerFromIndex(i) as DataGridRow; |
|
|
|
|
if (row == null) |
|
|
|
|
continue; |
|
|
|
|
|
|
|
|
|
SolidColorBrush brush = new SolidColorBrush(Colors.Transparent); |
|
|
|
|
if (this.Students[i].Name.Equals(student.Name)) |
|
|
|
|
brush = new SolidColorBrush(Colors.LightCoral); |
|
|
|
|
|
|
|
|
|
row.Background = brush; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
protected override void OnAttached() |
|
|
|
|
{ |
|
|
|
|
this.AssociatedObject.MouseUp += OnDataGridMouseDown; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
protected override void OnDetaching() |
|
|
|
|
{ |
|
|
|
|
this.AssociatedObject.MouseUp += OnDataGridMouseDown; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |