datagrid behaviors mouse down

main
syneffort 2 years ago
parent 8fe7d30387
commit 5a55141ab4
  1. 9
      PacticeSolution/DataGridEventTriggerSample/App.xaml
  2. 17
      PacticeSolution/DataGridEventTriggerSample/App.xaml.cs
  3. 10
      PacticeSolution/DataGridEventTriggerSample/AssemblyInfo.cs
  4. 14
      PacticeSolution/DataGridEventTriggerSample/DataGridEventTriggerSample.csproj
  5. 30
      PacticeSolution/DataGridEventTriggerSample/MainWindow.xaml
  6. 28
      PacticeSolution/DataGridEventTriggerSample/MainWindow.xaml.cs
  7. 15
      PacticeSolution/DataGridEventTriggerSample/Model/Student.cs
  8. 102
      PacticeSolution/DataGridEventTriggerSample/ViewModel/StudentViewModel.cs
  9. 12
      PacticeSolution/PacticeSolution.sln

@ -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>

@ -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
{
}
}

@ -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>

@ -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();
}
}
}

@ -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;
}
}
}

@ -17,11 +17,13 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ResourceSample", "ResourceS
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MVVMDatabaseSample", "MVVMDatabaseSample\MVVMDatabaseSample.csproj", "{B0A8E6DE-C6A2-488E-A148-9E7812F615F2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MVVMTextInputDetection", "MVVMTextInputDetection\MVVMTextInputDetection.csproj", "{C55A5081-B142-4120-8F62-3991276EF5C2}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MVVMTextInputDetection", "MVVMTextInputDetection\MVVMTextInputDetection.csproj", "{C55A5081-B142-4120-8F62-3991276EF5C2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MVVMCanvasRectangle", "MVVMCanvasRectangle\MVVMCanvasRectangle.csproj", "{C7A96511-E6B9-4F80-9C7B-3ABF4FC0AA4B}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MVVMCanvasRectangle", "MVVMCanvasRectangle\MVVMCanvasRectangle.csproj", "{C7A96511-E6B9-4F80-9C7B-3ABF4FC0AA4B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MVVMButtonControl", "MVVMButtonControl\MVVMButtonControl.csproj", "{8FE48C46-5529-4D0D-A96E-01A5A79B0B18}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MVVMButtonControl", "MVVMButtonControl\MVVMButtonControl.csproj", "{8FE48C46-5529-4D0D-A96E-01A5A79B0B18}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DataGridEventTriggerSample", "DataGridEventTriggerSample\DataGridEventTriggerSample.csproj", "{48CCBA2F-A583-4DAF-9F64-B9642CDB49FF}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -69,6 +71,10 @@ Global
{8FE48C46-5529-4D0D-A96E-01A5A79B0B18}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8FE48C46-5529-4D0D-A96E-01A5A79B0B18}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8FE48C46-5529-4D0D-A96E-01A5A79B0B18}.Release|Any CPU.Build.0 = Release|Any CPU
{48CCBA2F-A583-4DAF-9F64-B9642CDB49FF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{48CCBA2F-A583-4DAF-9F64-B9642CDB49FF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{48CCBA2F-A583-4DAF-9F64-B9642CDB49FF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{48CCBA2F-A583-4DAF-9F64-B9642CDB49FF}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

Loading…
Cancel
Save