datagrid behaviors mouse down
This commit is contained in:
9
PacticeSolution/DataGridEventTriggerSample/App.xaml
Normal file
9
PacticeSolution/DataGridEventTriggerSample/App.xaml
Normal file
@@ -0,0 +1,9 @@
|
||||
<Application x:Class="DataGridEventTriggerSample.App"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:local="clr-namespace:DataGridEventTriggerSample"
|
||||
StartupUri="MainWindow.xaml">
|
||||
<Application.Resources>
|
||||
|
||||
</Application.Resources>
|
||||
</Application>
|
17
PacticeSolution/DataGridEventTriggerSample/App.xaml.cs
Normal file
17
PacticeSolution/DataGridEventTriggerSample/App.xaml.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Configuration;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
|
||||
namespace DataGridEventTriggerSample
|
||||
{
|
||||
/// <summary>
|
||||
/// Interaction logic for App.xaml
|
||||
/// </summary>
|
||||
public partial class App : Application
|
||||
{
|
||||
}
|
||||
}
|
10
PacticeSolution/DataGridEventTriggerSample/AssemblyInfo.cs
Normal file
10
PacticeSolution/DataGridEventTriggerSample/AssemblyInfo.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using System.Windows;
|
||||
|
||||
[assembly: ThemeInfo(
|
||||
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
|
||||
//(used if a resource is not found in the page,
|
||||
// or application resource dictionaries)
|
||||
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
|
||||
//(used if a resource is not found in the page,
|
||||
// app, or any theme specific resource dictionaries)
|
||||
)]
|
@@ -0,0 +1,14 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>WinExe</OutputType>
|
||||
<TargetFramework>net6.0-windows</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<UseWPF>true</UseWPF>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Xaml.Behaviors.Wpf" Version="1.1.39" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
30
PacticeSolution/DataGridEventTriggerSample/MainWindow.xaml
Normal file
30
PacticeSolution/DataGridEventTriggerSample/MainWindow.xaml
Normal file
@@ -0,0 +1,30 @@
|
||||
<Window x:Class="DataGridEventTriggerSample.MainWindow"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:local="clr-namespace:DataGridEventTriggerSample" xmlns:viewmodel="clr-namespace:DataGridEventTriggerSample.ViewModel"
|
||||
xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
|
||||
mc:Ignorable="d"
|
||||
Title="MainWindow" Height="450" Width="800">
|
||||
|
||||
<Window.DataContext>
|
||||
<viewmodel:StudentViewModel/>
|
||||
</Window.DataContext>
|
||||
|
||||
<StackPanel>
|
||||
<DataGrid Name="grdMain"
|
||||
AutoGenerateColumns="True"
|
||||
IsReadOnly="True"
|
||||
VerticalAlignment="Stretch"
|
||||
HorizontalAlignment="Stretch"
|
||||
ItemsSource="{Binding Students, UpdateSourceTrigger=PropertyChanged}"
|
||||
ScrollViewer.CanContentScroll="True"
|
||||
ScrollViewer.HorizontalScrollBarVisibility="Visible"
|
||||
ScrollViewer.VerticalScrollBarVisibility="Auto">
|
||||
<i:Interaction.Behaviors>
|
||||
<viewmodel:StudentViewModel MouseDownCommand="{Binding MouseDownCommand}"/>
|
||||
</i:Interaction.Behaviors>
|
||||
</DataGrid>
|
||||
</StackPanel>
|
||||
</Window>
|
@@ -0,0 +1,28 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Data;
|
||||
using System.Windows.Documents;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Media.Imaging;
|
||||
using System.Windows.Navigation;
|
||||
using System.Windows.Shapes;
|
||||
|
||||
namespace DataGridEventTriggerSample
|
||||
{
|
||||
/// <summary>
|
||||
/// Interaction logic for MainWindow.xaml
|
||||
/// </summary>
|
||||
public partial class MainWindow : Window
|
||||
{
|
||||
public MainWindow()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
}
|
||||
}
|
15
PacticeSolution/DataGridEventTriggerSample/Model/Student.cs
Normal file
15
PacticeSolution/DataGridEventTriggerSample/Model/Student.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DataGridEventTriggerSample.Model
|
||||
{
|
||||
internal class Student
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public int Age { get; set; }
|
||||
public string Address { get; set; }
|
||||
}
|
||||
}
|
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user