Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修正AduDataGrid中使用AduCheckBox时,选中行后,复选框显示不正常的BUG #29

Open
superwoo opened this issue Nov 7, 2021 · 0 comments

Comments

@superwoo
Copy link

superwoo commented Nov 7, 2021

using AduSkin.Controls.Helper;
using AduSkin.Utility.Element;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace AduSkin.Controls.Metro
{
public class AduDataGrid : DataGrid
{
///


/// 所有 AduCheckBox 对象名称。
///

private IEnumerable _chkns = null;

  public AduDataGrid()
  {
     Utility.Refresh(this);

     _chkns = FindVisualChildren<AduCheckBox>(this).
        Select(v => v.Name).Distinct();
  }

  static AduDataGrid()
  {
     ElementBase.DefaultStyle<AduDataGrid>(DefaultStyleKeyProperty);
  }

  protected override void OnSelectionChanged(SelectionChangedEventArgs e)
  {
     base.OnSelectionChanged(e);

     if (_chkns.Count() > 0)
     {
        var setters = this.RowStyle?.Setters;
        var bg = Color.FromRgb(255, 255, 255);
        var bb = Color.FromRgb(255, 255, 255);

        foreach (Setter setter in setters)
        {
           if (setter.Property.Name == "Background")
              bg = setter.Value.ToColor();
           else if (setter.Property.Name == "BorderBrush")
              bb = setter.Value.ToColor();
        }

        var scs = this.SelectedCells;

        for (int i = 0; i < scs.Count; i++)
        {
           if (scs[i].Column is DataGridTemplateColumn tc)
           {
              for (int j = 0; j < this.Items.Count; j++)
              {
                 var fe = tc.GetCellContent(this.Items[j]);

                 if (fe != null)
                 {
                    foreach (var item in _chkns)
                    {
                       if (tc.CellTemplate.FindName(item, fe) is AduCheckBox chk)
                       {
                          chk.Foreground = j == this.SelectedIndex ?
                             new SolidColorBrush(bg) : new SolidColorBrush(bb);
                       }
                    }
                 }
              }
           }
        }
     }
  }

  private static IEnumerable<T> FindVisualChildren<T>(DependencyObject dep) where T : DependencyObject
  {
     if (dep != null)
     {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(dep); i++)
        {
           DependencyObject child = VisualTreeHelper.GetChild(dep, i);
           if (child != null && child is T)
           {
              yield return (T)child;
           }

           foreach (T childOfChild in FindVisualChildren<T>(child))
           {
              yield return childOfChild;
           }
        }
     }
  }

}
}
// ExtensionMethod
internal static Color ToColor(this object value)
{
try
{
return (Color)ColorConverter.ConvertFromString(value.ToString());
}
catch (System.Exception)
{
return Color.FromRgb(255, 255, 255);
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant