-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
详细请看 [WPF 给 Grid 的辅助方法 添加行列名称绑定](https://lindexi.gitee.io/post/WPF-%E7%BB%99-Grid-%E7%9A%84%E8%BE%85%E5%8A%A9%E6%96%B9%E6%B3%95-%E6%B7%BB%E5%8A%A0%E8%A1%8C%E5%88%97%E5%90%8D%E7%A7%B0%E7%BB%91%E5%AE%9A.html )
- Loading branch information
Showing
2 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
src/Shared/HandyControl_Shared/Tools/Extension/GridExtension.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
using System; | ||
using System.Windows; | ||
using System.Windows.Controls; | ||
|
||
namespace HandyControl.Tools.Extension | ||
{ | ||
public class GridExtensions | ||
{ | ||
public static readonly DependencyProperty NameProperty = DependencyProperty.RegisterAttached( | ||
"Name", typeof(string), typeof(GridExtensions), new PropertyMetadata(default(string))); | ||
|
||
public static void SetName(DependencyObject element, string value) | ||
{ | ||
element.SetValue(NameProperty, value); | ||
} | ||
|
||
public static string GetName(DependencyObject element) | ||
{ | ||
return (string) element.GetValue(NameProperty); | ||
} | ||
|
||
public static readonly DependencyProperty RowNameProperty = DependencyProperty.RegisterAttached( | ||
"RowName", typeof(string), typeof(GridExtensions), | ||
new PropertyMetadata(default(string), RowName_PropertyChanged)); | ||
|
||
private static void RowName_PropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) | ||
{ | ||
if (d is FrameworkElement frameworkElement) | ||
{ | ||
if (e.NewValue is string rowName) | ||
{ | ||
if (string.IsNullOrEmpty(rowName)) | ||
{ | ||
return; | ||
} | ||
|
||
if (frameworkElement.Parent is Grid grid) | ||
{ | ||
for (var i = 0; i < grid.RowDefinitions.Count; i++) | ||
{ | ||
var gridRowDefinition = grid.RowDefinitions[i]; | ||
var gridRowName = GetName(gridRowDefinition); | ||
if (!string.IsNullOrEmpty(gridRowName) && | ||
gridRowName.Equals(rowName, StringComparison.Ordinal)) | ||
{ | ||
Grid.SetRow(frameworkElement, i); | ||
return; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
public static void SetRowName(DependencyObject element, string value) | ||
{ | ||
element.SetValue(RowNameProperty, value); | ||
} | ||
|
||
public static string GetRowName(DependencyObject element) | ||
{ | ||
return (string) element.GetValue(RowNameProperty); | ||
} | ||
|
||
public static readonly DependencyProperty ColumnNameProperty = DependencyProperty.RegisterAttached( | ||
"ColumnName", typeof(string), typeof(GridExtensions), | ||
new PropertyMetadata(default(string), ColumnName_PropertyChanged)); | ||
|
||
private static void ColumnName_PropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) | ||
{ | ||
if (d is FrameworkElement frameworkElement) | ||
{ | ||
if (e.NewValue is string columnName) | ||
{ | ||
if (string.IsNullOrEmpty(columnName)) | ||
{ | ||
return; | ||
} | ||
|
||
if (frameworkElement.Parent is Grid grid) | ||
{ | ||
for (var i = 0; i < grid.ColumnDefinitions.Count; i++) | ||
{ | ||
var gridColumnDefinition = grid.ColumnDefinitions[i]; | ||
var gridColumnName = GetName(gridColumnDefinition); | ||
if (!string.IsNullOrEmpty(gridColumnName) && | ||
gridColumnName.Equals(columnName, StringComparison.Ordinal)) | ||
{ | ||
Grid.SetColumn(frameworkElement, i); | ||
return; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
public static void SetColumnName(DependencyObject element, string value) | ||
{ | ||
element.SetValue(ColumnNameProperty, value); | ||
} | ||
|
||
public static string GetColumnName(DependencyObject element) | ||
{ | ||
return (string) element.GetValue(ColumnNameProperty); | ||
} | ||
} | ||
} |