258 lines
8.7 KiB
C#
258 lines
8.7 KiB
C#
// Copyright (c) 2017 schick Informatik
|
|
// Description: DataGrid mit etwas "verbesserten" Funktionen
|
|
//
|
|
|
|
using System;
|
|
using System.Windows;
|
|
using System.Windows.Controls;
|
|
using System.Windows.Input;
|
|
using System.Windows.Media;
|
|
using System.Windows.Media.Imaging;
|
|
|
|
using System.Windows.Controls.Primitives;
|
|
using System.Collections.Generic;
|
|
|
|
using System.Globalization;
|
|
using System.Threading;
|
|
|
|
namespace BreCalClient
|
|
{
|
|
/// <summary>
|
|
/// Follow steps 1a or 1b and then 2 to use this custom control in a XAML file.
|
|
///
|
|
/// Step 1a) Using this custom control in a XAML file that exists in the current project.
|
|
/// Add this XmlNamespace attribute to the root element of the markup file where it is
|
|
/// to be used:
|
|
///
|
|
/// xmlns:enictrl="clr-namespace:ENI2.Controls"
|
|
///
|
|
///
|
|
/// Step 1b) Using this custom control in a XAML file that exists in a different project.
|
|
/// Add this XmlNamespace attribute to the root element of the markup file where it is
|
|
/// to be used:
|
|
///
|
|
/// xmlns:enictrl="clr-namespace:ENI2.Controls;assembly=ENI2.Controls"
|
|
///
|
|
/// You will also need to add a project reference from the project where the XAML file lives
|
|
/// to this project and Rebuild to avoid compilation errors:
|
|
///
|
|
/// Right click on the target project in the Solution Explorer and
|
|
/// "Add Reference"->"Projects"->[Browse to and select this project]
|
|
///
|
|
///
|
|
/// Step 2)
|
|
/// Go ahead and use your control in the XAML file.
|
|
///
|
|
/// <MyNamespace:ENIDataGrid/>
|
|
///
|
|
/// </summary>
|
|
public class ENIDataGrid : DataGrid
|
|
{
|
|
|
|
public event Action<object>? EditRequested;
|
|
public event Action<object>? DeleteRequested;
|
|
public event Action? CreateRequested;
|
|
public event Action? RefreshGrid;
|
|
|
|
public event Action<object>? PrintRequested;
|
|
public event Action<object>? ExportRequested;
|
|
public event Action<object>? ShowTextRequested;
|
|
|
|
public void Initialize()
|
|
{
|
|
|
|
this.MouseDoubleClick += dataGrid_MouseDoubleClick;
|
|
this.PreviewKeyDown += ENIDataGrid_PreviewKeyDown;
|
|
|
|
this.ContextMenu = new ContextMenu();
|
|
this.CanUserAddRows = false;
|
|
this.IsReadOnly = false;
|
|
|
|
MenuItem addItem = new MenuItem();
|
|
|
|
addItem.Header = BreCalClient.Resources.Resources.textAdd;
|
|
addItem.Icon = new Image { Source = Util.LoadImage(BreCalClient.Resources.Resources.add) };
|
|
addItem.Click += new RoutedEventHandler(this.addItem);
|
|
this.ContextMenu.Items.Add(addItem);
|
|
|
|
MenuItem deleteItem = new MenuItem();
|
|
deleteItem.Header = BreCalClient.Resources.Resources.textDelete;
|
|
deleteItem.Icon = new Image { Source = Util.LoadImage(BreCalClient.Resources.Resources.delete) };
|
|
deleteItem.Click += this.deleteItem;
|
|
this.ContextMenu.Items.Add(deleteItem);
|
|
|
|
MenuItem editItem = new MenuItem();
|
|
editItem.Header = BreCalClient.Resources.Resources.textEdit;
|
|
editItem.Icon = new Image { Source = Util.LoadImage(BreCalClient.Resources.Resources.edit) };
|
|
editItem.Click += this.editItem;
|
|
this.ContextMenu.Items.Add(editItem);
|
|
}
|
|
|
|
private void ENIDataGrid_PreviewKeyDown(object sender, KeyEventArgs e)
|
|
{
|
|
if(sender is ENIDataGrid)
|
|
{
|
|
var grid = sender as ENIDataGrid;
|
|
if (Key.Delete == e.Key)
|
|
this.deleteItem(null, null);
|
|
}
|
|
}
|
|
|
|
#region public
|
|
|
|
public DataGridRow GetRow(int index)
|
|
{
|
|
DataGridRow row = (DataGridRow)this.ItemContainerGenerator.ContainerFromIndex(index);
|
|
if(row == null)
|
|
{
|
|
this.UpdateLayout();
|
|
this.ScrollIntoView(this.Items[index]);
|
|
row = (DataGridRow)this.ItemContainerGenerator.ContainerFromIndex(index);
|
|
}
|
|
return row;
|
|
}
|
|
|
|
public DataGridCell? GetCell(DataGridRow row, int column)
|
|
{
|
|
if (row != null)
|
|
{
|
|
DataGridCellsPresenter? presenter = GetVisualChild<DataGridCellsPresenter>(row);
|
|
|
|
if (presenter == null)
|
|
{
|
|
this.ScrollIntoView(row, this.Columns[column]);
|
|
presenter = GetVisualChild<DataGridCellsPresenter>(row);
|
|
}
|
|
|
|
DataGridCell? cell = (DataGridCell?)presenter?.ItemContainerGenerator.ContainerFromIndex(column);
|
|
return cell;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public DataGridCell? GetCell(int rowIndex, int columnIndex)
|
|
{
|
|
DataGridRow row = this.GetRow(rowIndex);
|
|
return this.GetCell(row, columnIndex);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region protected
|
|
|
|
protected void addItem(object sender, RoutedEventArgs e)
|
|
{
|
|
if (!this.IsReadOnly)
|
|
{
|
|
this.CreateRequested?.Invoke();
|
|
}
|
|
e.Handled = true;
|
|
}
|
|
|
|
protected void deleteItem(object? sender, RoutedEventArgs? e)
|
|
{
|
|
if((this.SelectedItems != null) && (this.SelectedItems.Count > 0) && !this.IsReadOnly)
|
|
{
|
|
MessageBoxResult result = MessageBox.Show("Are your sure?", "Please confirm", MessageBoxButton.YesNo, MessageBoxImage.Question);
|
|
if (result == MessageBoxResult.Yes)
|
|
{
|
|
List<object> deleteList = new List<object>();
|
|
foreach (object deleteItem in this.SelectedItems)
|
|
deleteList.Add(deleteItem);
|
|
|
|
foreach (object deleteItem in deleteList)
|
|
{
|
|
if (deleteItem != null)
|
|
{
|
|
this.DeleteRequested?.Invoke(deleteItem);
|
|
}
|
|
}
|
|
|
|
this.RefreshGrid?.Invoke();
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
protected void editItem(object sender, RoutedEventArgs e)
|
|
{
|
|
if((this.SelectedItems != null) && (this.SelectedItems.Count == 1) && !this.IsReadOnly)
|
|
{
|
|
if (this.SelectedItems[0] is object selectedEntity)
|
|
this.EditRequested?.Invoke(selectedEntity);
|
|
}
|
|
}
|
|
|
|
protected void printItem(object sender, RoutedEventArgs e)
|
|
{
|
|
if ((this.SelectedItems != null) && (this.SelectedItems.Count == 1) )
|
|
{
|
|
if (this.SelectedItems[0] is object selectedEntity)
|
|
this.PrintRequested?.Invoke(selectedEntity);
|
|
}
|
|
}
|
|
|
|
protected void exportItem(object sender, RoutedEventArgs e)
|
|
{
|
|
if ((this.SelectedItems != null) && (this.SelectedItems.Count == 1))
|
|
{
|
|
if (this.SelectedItems[0] is object selectedEntity)
|
|
this.ExportRequested?.Invoke(selectedEntity);
|
|
}
|
|
}
|
|
|
|
protected void showTextItem(object sender, RoutedEventArgs e)
|
|
{
|
|
if ((this.SelectedItems != null) && (this.SelectedItems.Count == 1))
|
|
{
|
|
if (this.SelectedItems[0] is object selectedEntity)
|
|
this.ShowTextRequested?.Invoke(selectedEntity);
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region private
|
|
|
|
private void dataGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
|
|
{
|
|
if (sender != null)
|
|
{
|
|
if ((sender is DataGrid grid) && (grid.SelectedItems != null) && (grid.SelectedItems.Count == 1) && !this.IsReadOnly)
|
|
{
|
|
DataGridRow? dgr = grid.ItemContainerGenerator.ContainerFromItem(grid.SelectedItem) as DataGridRow;
|
|
if (grid.SelectedItem is object selectedEntity)
|
|
this.EditRequested?.Invoke(selectedEntity);
|
|
}
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region private static
|
|
|
|
private static T? GetVisualChild<T>(Visual parent) where T : Visual
|
|
{
|
|
T? child = default;
|
|
int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
|
|
for (int i = 0; i < numVisuals; i++)
|
|
{
|
|
Visual? v = (Visual)VisualTreeHelper.GetChild(parent, i);
|
|
child = v as T;
|
|
if (child == null)
|
|
{
|
|
child = GetVisualChild<T>(v);
|
|
}
|
|
if (child != null)
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
return child;
|
|
}
|
|
|
|
#endregion
|
|
|
|
}
|
|
}
|