Skip to content

Commit 4075758

Browse files
5990208159902081
authored andcommitted
代码优化;
1 parent de82fba commit 4075758

File tree

8 files changed

+268
-270
lines changed

8 files changed

+268
-270
lines changed

One.Control/AttachDP/GridAttach.cs

Lines changed: 76 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -2,115 +2,114 @@
22
using System.Windows;
33
using System.Windows.Controls;
44

5-
namespace One.Control.AttachDP
5+
namespace One.Control.AttachDP;
6+
7+
public class GridAttach
68
{
7-
public class GridAttach
8-
{
9-
public static readonly DependencyProperty NameProperty = DependencyProperty.RegisterAttached(
10-
"Name", typeof(string), typeof(GridAttach), new PropertyMetadata(default(string)));
9+
public static readonly DependencyProperty NameProperty = DependencyProperty.RegisterAttached(
10+
"Name", typeof(string), typeof(GridAttach), new PropertyMetadata(default(string)));
1111

12-
public static void SetName(DependencyObject element, string value)
13-
{
14-
element.SetValue(NameProperty, value);
15-
}
12+
public static void SetName(DependencyObject element, string value)
13+
{
14+
element.SetValue(NameProperty, value);
15+
}
1616

17-
public static string GetName(DependencyObject element)
18-
{
19-
return (string)element.GetValue(NameProperty);
20-
}
17+
public static string GetName(DependencyObject element)
18+
{
19+
return (string)element.GetValue(NameProperty);
20+
}
2121

22-
public static readonly DependencyProperty RowNameProperty = DependencyProperty.RegisterAttached(
23-
"RowName", typeof(string), typeof(GridAttach),
24-
new PropertyMetadata(default(string), RowName_PropertyChanged));
22+
public static readonly DependencyProperty RowNameProperty = DependencyProperty.RegisterAttached(
23+
"RowName", typeof(string), typeof(GridAttach),
24+
new PropertyMetadata(default(string), RowName_PropertyChanged));
2525

26-
private static void RowName_PropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
26+
private static void RowName_PropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
27+
{
28+
if (d is FrameworkElement frameworkElement)
2729
{
28-
if (d is FrameworkElement frameworkElement)
30+
if (e.NewValue is string rowName)
2931
{
30-
if (e.NewValue is string rowName)
32+
if (string.IsNullOrEmpty(rowName))
3133
{
32-
if (string.IsNullOrEmpty(rowName))
33-
{
34-
return;
35-
}
34+
return;
35+
}
3636

37-
if (frameworkElement.Parent is Grid grid)
37+
if (frameworkElement.Parent is Grid grid)
38+
{
39+
for (var i = 0; i < grid.RowDefinitions.Count; i++)
3840
{
39-
for (var i = 0; i < grid.RowDefinitions.Count; i++)
41+
var gridRowDefinition = grid.RowDefinitions[i];
42+
var gridRowName = GetName(gridRowDefinition);
43+
if (!string.IsNullOrEmpty(gridRowName) &&
44+
gridRowName.Equals(rowName, StringComparison.Ordinal))
4045
{
41-
var gridRowDefinition = grid.RowDefinitions[i];
42-
var gridRowName = GetName(gridRowDefinition);
43-
if (!string.IsNullOrEmpty(gridRowName) &&
44-
gridRowName.Equals(rowName, StringComparison.Ordinal))
45-
{
46-
Grid.SetRow(frameworkElement, i);
47-
return;
48-
}
46+
Grid.SetRow(frameworkElement, i);
47+
return;
4948
}
5049
}
51-
else
52-
{
53-
throw new ArgumentException("只有在Grid容器内才能设置 RowName 附加属性");
54-
}
50+
}
51+
else
52+
{
53+
throw new ArgumentException("只有在Grid容器内才能设置 RowName 附加属性");
5554
}
5655
}
5756
}
57+
}
5858

59-
public static void SetRowName(DependencyObject element, string value)
60-
{
61-
element.SetValue(RowNameProperty, value);
62-
}
59+
public static void SetRowName(DependencyObject element, string value)
60+
{
61+
element.SetValue(RowNameProperty, value);
62+
}
6363

64-
public static string GetRowName(DependencyObject element)
65-
{
66-
return (string)element.GetValue(RowNameProperty);
67-
}
64+
public static string GetRowName(DependencyObject element)
65+
{
66+
return (string)element.GetValue(RowNameProperty);
67+
}
6868

69-
public static readonly DependencyProperty ColumnNameProperty = DependencyProperty.RegisterAttached(
70-
"ColumnName", typeof(string), typeof(GridAttach),
71-
new PropertyMetadata(default(string), ColumnName_PropertyChanged));
69+
public static readonly DependencyProperty ColumnNameProperty = DependencyProperty.RegisterAttached(
70+
"ColumnName", typeof(string), typeof(GridAttach),
71+
new PropertyMetadata(default(string), ColumnName_PropertyChanged));
7272

73-
private static void ColumnName_PropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
73+
private static void ColumnName_PropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
74+
{
75+
if (d is FrameworkElement frameworkElement)
7476
{
75-
if (d is FrameworkElement frameworkElement)
77+
if (e.NewValue is string columnName)
7678
{
77-
if (e.NewValue is string columnName)
79+
if (string.IsNullOrEmpty(columnName))
7880
{
79-
if (string.IsNullOrEmpty(columnName))
80-
{
81-
return;
82-
}
81+
return;
82+
}
8383

84-
if (frameworkElement.Parent is Grid grid)
84+
if (frameworkElement.Parent is Grid grid)
85+
{
86+
for (var i = 0; i < grid.ColumnDefinitions.Count; i++)
8587
{
86-
for (var i = 0; i < grid.ColumnDefinitions.Count; i++)
88+
var gridColumnDefinition = grid.ColumnDefinitions[i];
89+
var gridColumnName = GetName(gridColumnDefinition);
90+
if (!string.IsNullOrEmpty(gridColumnName) &&
91+
gridColumnName.Equals(columnName, StringComparison.Ordinal))
8792
{
88-
var gridColumnDefinition = grid.ColumnDefinitions[i];
89-
var gridColumnName = GetName(gridColumnDefinition);
90-
if (!string.IsNullOrEmpty(gridColumnName) &&
91-
gridColumnName.Equals(columnName, StringComparison.Ordinal))
92-
{
93-
Grid.SetColumn(frameworkElement, i);
94-
return;
95-
}
93+
Grid.SetColumn(frameworkElement, i);
94+
return;
9695
}
9796
}
98-
else
99-
{
100-
throw new ArgumentException("只有在Grid容器内才能设置 ColumnName 附加属性");
101-
}
97+
}
98+
else
99+
{
100+
throw new ArgumentException("只有在Grid容器内才能设置 ColumnName 附加属性");
102101
}
103102
}
104103
}
104+
}
105105

106-
public static void SetColumnName(DependencyObject element, string value)
107-
{
108-
element.SetValue(ColumnNameProperty, value);
109-
}
106+
public static void SetColumnName(DependencyObject element, string value)
107+
{
108+
element.SetValue(ColumnNameProperty, value);
109+
}
110110

111-
public static string GetColumnName(DependencyObject element)
112-
{
113-
return (string)element.GetValue(ColumnNameProperty);
114-
}
111+
public static string GetColumnName(DependencyObject element)
112+
{
113+
return (string)element.GetValue(ColumnNameProperty);
115114
}
116115
}

One.Control/AttachDP/SelectedItemsAttach.cs

Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,59 +2,58 @@
22
using System.Windows;
33
using System.Windows.Controls;
44

5-
namespace One.Control.AttachDP
5+
namespace One.Control.AttachDP;
6+
7+
public class SelectedItemsAttach
68
{
7-
public class SelectedItemsAttach
9+
public static IList GetSelectedItems(DependencyObject obj)
810
{
9-
public static IList GetSelectedItems(DependencyObject obj)
10-
{
11-
return (IList)obj.GetValue(SelectedItemsProperty);
12-
}
11+
return (IList)obj.GetValue(SelectedItemsProperty);
12+
}
1313

14-
public static void SetSelectedItems(DependencyObject obj, IList value)
15-
{
16-
obj.SetValue(SelectedItemsProperty, value);
17-
}
14+
public static void SetSelectedItems(DependencyObject obj, IList value)
15+
{
16+
obj.SetValue(SelectedItemsProperty, value);
17+
}
1818

19-
//Using a DependencyProperty as the backing store for SelectedItems. This enables animation, styling, binding, etc...
20-
public static readonly DependencyProperty SelectedItemsProperty =
21-
DependencyProperty.RegisterAttached("SelectedItems", typeof(IList), typeof(SelectedItemsAttach), new PropertyMetadata(OnSelectedItemsChanged));
19+
//Using a DependencyProperty as the backing store for SelectedItems. This enables animation, styling, binding, etc...
20+
public static readonly DependencyProperty SelectedItemsProperty =
21+
DependencyProperty.RegisterAttached("SelectedItems", typeof(IList), typeof(SelectedItemsAttach), new PropertyMetadata(OnSelectedItemsChanged));
2222

23-
public static void OnSelectedItemsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
23+
public static void OnSelectedItemsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
24+
{
25+
var listBox = d as ListBox;
26+
if ((listBox != null) && (listBox.SelectionMode == SelectionMode.Multiple))
2427
{
25-
var listBox = d as ListBox;
26-
if ((listBox != null) && (listBox.SelectionMode == SelectionMode.Multiple))
28+
if (e.OldValue != null)
2729
{
28-
if (e.OldValue != null)
29-
{
30-
listBox.SelectionChanged -= OnlistBoxSelectionChanged;
31-
}
32-
IList collection = e.NewValue as IList;
33-
listBox.SelectedItems.Clear();
34-
if (collection != null)
30+
listBox.SelectionChanged -= OnlistBoxSelectionChanged;
31+
}
32+
IList collection = e.NewValue as IList;
33+
listBox.SelectedItems.Clear();
34+
if (collection != null)
35+
{
36+
foreach (var item in collection)
3537
{
36-
foreach (var item in collection)
37-
{
38-
listBox.SelectedItems.Add(item);
39-
}
40-
listBox.SelectionChanged += OnlistBoxSelectionChanged;
38+
listBox.SelectedItems.Add(item);
4139
}
40+
listBox.SelectionChanged += OnlistBoxSelectionChanged;
4241
}
4342
}
43+
}
4444

45-
private static void OnlistBoxSelectionChanged(object sender, SelectionChangedEventArgs e)
45+
private static void OnlistBoxSelectionChanged(object sender, SelectionChangedEventArgs e)
46+
{
47+
IList dataSource = GetSelectedItems(sender as DependencyObject);
48+
//添加用户选中的当前项.
49+
foreach (var item in e.AddedItems)
4650
{
47-
IList dataSource = GetSelectedItems(sender as DependencyObject);
48-
//添加用户选中的当前项.
49-
foreach (var item in e.AddedItems)
50-
{
51-
dataSource.Add(item);
52-
}
53-
//删除用户取消选中的当前项
54-
foreach (var item in e.RemovedItems)
55-
{
56-
dataSource.Remove(item);
57-
}
51+
dataSource.Add(item);
52+
}
53+
//删除用户取消选中的当前项
54+
foreach (var item in e.RemovedItems)
55+
{
56+
dataSource.Remove(item);
5857
}
5958
}
6059
}
Lines changed: 38 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,47 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
6-
using System.Windows.Interop;
1+
using System.Windows.Interop;
72

83
using Windows.Win32;
94

10-
namespace One.Toolbox.Helpers
5+
namespace One.Toolbox.Helpers;
6+
7+
public class WindowHelper
118
{
12-
public class WindowHelper
9+
/// <summary>让窗口激活作为前台最上层窗口</summary>
10+
/// <param name="window"></param>
11+
public static unsafe void SetWindowToForeground(Window window)
1312
{
14-
/// <summary> 让窗口激活作为前台最上层窗口 </summary>
15-
/// <param name="window"> </param>
16-
public static unsafe void SetWindowToForeground(Window window)
13+
// [WPF 让窗口激活作为前台最上层窗口的方法 - lindexi - 博客园](https://www.cnblogs.com/lindexi/p/12749671.html)
14+
var interopHelper = new WindowInteropHelper(window);
15+
16+
uint* aa = (uint*)IntPtr.Zero;
17+
var thisWindowThreadId = PInvoke.GetWindowThreadProcessId(new Windows.Win32.Foundation.HWND(interopHelper.Handle), aa);
18+
var currentForegroundWindow = PInvoke.GetForegroundWindow();
19+
var currentForegroundWindowThreadId = PInvoke.GetWindowThreadProcessId(currentForegroundWindow, aa);
20+
21+
// [c# - Bring a window to the front in WPF - Stack
22+
// Overflow](https://stackoverflow.com/questions/257587/bring-a-window-to-the-front-in-wpf )
23+
// [SetForegroundWindow的正确用法 - 子坞 -
24+
// 博客园](https://www.cnblogs.com/ziwuge/archive/2012/01/06/2315342.html )
25+
/*
26+
1.得到窗口句柄FindWindow
27+
2.切换键盘输入焦点AttachThreadInput
28+
3.显示窗口ShowWindow(有些窗口被最小化/隐藏了)
29+
4.更改窗口的Z Order,SetWindowPos使之最上,为了不影响后续窗口的Z Order,改完之后,再还原
30+
5.最后SetForegroundWindow
31+
*/
32+
33+
PInvoke.AttachThreadInput(currentForegroundWindowThreadId, thisWindowThreadId, true);
34+
35+
window.Show();
36+
window.Activate();
37+
// 去掉和其他线程的输入链接
38+
PInvoke.AttachThreadInput(currentForegroundWindowThreadId, thisWindowThreadId, false);
39+
40+
// 用于踢掉其他的在上层的窗口
41+
if (window.Topmost != true)
1742
{
18-
// [WPF 让窗口激活作为前台最上层窗口的方法 - lindexi - 博客园](https://www.cnblogs.com/lindexi/p/12749671.html)
19-
var interopHelper = new WindowInteropHelper(window);
20-
21-
uint* aa = (uint*)IntPtr.Zero;
22-
var thisWindowThreadId = PInvoke.GetWindowThreadProcessId(new Windows.Win32.Foundation.HWND(interopHelper.Handle), aa);
23-
var currentForegroundWindow = PInvoke.GetForegroundWindow();
24-
var currentForegroundWindowThreadId = PInvoke.GetWindowThreadProcessId(currentForegroundWindow, aa);
25-
26-
// [c# - Bring a window to the front in WPF - Stack Overflow](https://stackoverflow.com/questions/257587/bring-a-window-to-the-front-in-wpf ) [SetForegroundWindow的正确用法 - 子坞 - 博客园](https://www.cnblogs.com/ziwuge/archive/2012/01/06/2315342.html )
27-
/*
28-
1.得到窗口句柄FindWindow
29-
2.切换键盘输入焦点AttachThreadInput
30-
3.显示窗口ShowWindow(有些窗口被最小化/隐藏了)
31-
4.更改窗口的Z Order,SetWindowPos使之最上,为了不影响后续窗口的Z Order,改完之后,再还原
32-
5.最后SetForegroundWindow
33-
*/
34-
35-
PInvoke.AttachThreadInput(currentForegroundWindowThreadId, thisWindowThreadId, true);
36-
37-
window.Show();
38-
window.Activate();
39-
// 去掉和其他线程的输入链接
40-
PInvoke.AttachThreadInput(currentForegroundWindowThreadId, thisWindowThreadId, false);
41-
42-
// 用于踢掉其他的在上层的窗口
43-
if (window.Topmost != true)
44-
{
45-
window.Topmost = true;
46-
window.Topmost = false;
47-
}
43+
window.Topmost = true;
44+
window.Topmost = false;
4845
}
4946
}
5047
}

0 commit comments

Comments
 (0)