Add RenewContext

This commit is contained in:
2023-05-25 18:03:38 +02:00
parent 27abf559e8
commit 7468af3970
9 changed files with 177 additions and 43 deletions

View File

@ -10,6 +10,7 @@ using System.Windows.Controls.Primitives;
using System.Text.RegularExpressions;
using System.IO.Ports;
using System.Net.Sockets;
using System.Collections;
namespace Elwig.Helpers {
public static partial class Utils {
@ -129,6 +130,40 @@ namespace Elwig.Helpers {
return null;
}
public static void RenewItemsSource(Selector selector, IEnumerable? source, Func<object?, object?> getId) {
var selectedId = getId(selector.SelectedItem);
selector.ItemsSource = source;
if (selectedId != null && source != null)
selector.SelectedItem = source.Cast<object>().FirstOrDefault(i => selectedId.Equals(getId(i)));
}
public static void RenewItemsSource(Xceed.Wpf.Toolkit.Primitives.Selector selector, IEnumerable? source, Func<object?, object?> getId) {
var selectedIds = selector.SelectedItems.Cast<object>().Select(i => getId(i)).ToList();
selector.ItemsSource = source;
if (source != null) {
foreach (var i in source.Cast<object>().Where(i => selectedIds.Contains(getId(i))))
selector.SelectedItems.Add(i);
}
}
public static void RenewItemsSource(DataGrid dataGrid, IEnumerable? source, Func<object?, object?> getId, bool keepSort = true) {
var column = dataGrid.CurrentCell.Column;
var sortColumns = dataGrid.Columns.Select(c => c.SortDirection).ToList();
var sort = dataGrid.Items.SortDescriptions.ToList();
var selectedId = getId(dataGrid.SelectedItem);
dataGrid.ItemsSource = source;
if (keepSort) {
for (int i = 0; i < dataGrid.Columns.Count; i++)
dataGrid.Columns[i].SortDirection = sortColumns[i];
foreach (var s in sort)
dataGrid.Items.SortDescriptions.Add(s);
}
if (selectedId != null && source != null)
dataGrid.SelectedItem = source.Cast<object>().FirstOrDefault(i => selectedId.Equals(getId(i)));
if (dataGrid.SelectedItem != null)
dataGrid.CurrentCell = new(dataGrid.SelectedItem, column);
}
public static int Modulo(string a, int b) {
if (a.Length == 0 || !a.All(char.IsAsciiDigit)) {
throw new ArgumentException("First argument has to be a decimal string");