-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathWpfListOfThings.cs
74 lines (69 loc) · 2.47 KB
/
WpfListOfThings.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Markup;
using System.Windows.Media;
namespace WPF_dnscrypt_proxy_md
{
public class IndexConverter : MarkupExtension, IValueConverter
{
public object Convert(object value, Type TargetType, object parameter, CultureInfo culture)
{
var item = (ListViewItem)value;
var listView = ItemsControl.ItemsControlFromItemContainer(item) as ListView;
var index = listView.ItemContainerGenerator.IndexFromContainer(item) + 1;
return index.ToString();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
}
public static class Extensions
{
public static IEnumerable<ChildT> FindVisualChild<ChildT>(this DependencyObject obj) where ChildT : DependencyObject
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(obj, i);
if (child is ChildT ct)
yield return ct;
else
{
foreach (var item in FindVisualChild<ChildT>(child))
{
yield return item;
}
}
}
}
}
public class NamesRule : ValidationRule
{
public const string Error = "duplicate name";
public ListView Source { get; set; }
public object Current { get; set; }
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
if (null != cultureInfo)
(App.Current.MainWindow as MainWindow).ValidateListViewItems();
foreach (dynamic item in Source.Items)
{
if (value is BindingExpression be && be.DataItem is ExpandoObject eo)
{
if (item != be.DataItem && item.Name == (string)((dynamic)(eo)).Name)
return new ValidationResult(false, Error);
}
}
return ValidationResult.ValidResult;
}
}
}