Skip to content

Commit

Permalink
Merge pull request #770 from dotnet-campus/t/lindexi/GridExtensions
Browse files Browse the repository at this point in the history
加上给 Grid 行列命名绑定的辅助方法
  • Loading branch information
NaBian authored Jun 21, 2021
2 parents 667d0d5 + 16c3399 commit 0848d2b
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Tools\Converter\ThicknessSplitConverter.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Tools\Extension\DependencyObjectExtension.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Tools\Extension\FrameworkElementExtension.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Tools\Extension\GridExtension.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Tools\Extension\LangExtension.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Tools\Extension\StringExtension.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Tools\Extension\ValueExtension.cs" />
Expand Down
108 changes: 108 additions & 0 deletions src/Shared/HandyControl_Shared/Tools/Extension/GridExtension.cs
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);
}
}
}

0 comments on commit 0848d2b

Please sign in to comment.