diff --git a/HandyControl/Controls/Attach/DataGridAttach.cs b/HandyControl/Controls/Attach/DataGridAttach.cs index f122cab3..68416965 100644 --- a/HandyControl/Controls/Attach/DataGridAttach.cs +++ b/HandyControl/Controls/Attach/DataGridAttach.cs @@ -1,6 +1,8 @@ -using System.Linq; +using System.Linq; using System.Windows; using System.Windows.Controls; +using System.Windows.Controls.Primitives; +using HandyControl.Data; namespace HandyControl.Controls { @@ -206,5 +208,67 @@ namespace HandyControl.Controls } } } + + public static DependencyProperty ShowRowNumberProperty = DependencyProperty.RegisterAttached("ShowRowNumber", + typeof(bool), typeof(DataGridAttach), + new FrameworkPropertyMetadata(ValueBoxes.FalseBox, FrameworkPropertyMetadataOptions.Inherits, OnShowRowNumberChanged)); + + public static bool GetShowRowNumber(DependencyObject target) => (bool)target.GetValue(ShowRowNumberProperty); + + public static void SetShowRowNumber(DependencyObject target, bool value) => target.SetValue(ShowRowNumberProperty, value); + + private static void OnShowRowNumberChanged(DependencyObject target, DependencyPropertyChangedEventArgs e) + { + if (!(target is DataGrid dataGrid)) return; + var show = (bool)e.NewValue; + + if (show) + { + dataGrid.LoadingRow += DataGrid_LoadingRow; + dataGrid.ItemContainerGenerator.ItemsChanged += ItemContainerGenerator_ItemsChanged; + } + else + { + dataGrid.LoadingRow -= DataGrid_LoadingRow; + dataGrid.ItemContainerGenerator.ItemsChanged -= ItemContainerGenerator_ItemsChanged; + } + + UpdateItems(dataGrid.ItemContainerGenerator, show); + } + + private static void ItemContainerGenerator_ItemsChanged(object sender, ItemsChangedEventArgs e) + { + if (!(sender is ItemContainerGenerator generator)) return; + UpdateItems(generator); + } + + private static void UpdateItems(ItemContainerGenerator generator, bool show = true) + { + var count = generator.Items.Count; + if (show) + { + for (int i = 0; i < count; i++) + { + var row = (DataGridRow)generator.ContainerFromIndex(i); + if (row != null) + { + row.Header = (i + 1).ToString(); + } + } + } + else + { + for (int i = 0; i < count; i++) + { + var row = (DataGridRow)generator.ContainerFromIndex(i); + if (row != null) + { + row.Header = null; + } + } + } + } + + private static void DataGrid_LoadingRow(object sender, DataGridRowEventArgs e) => e.Row.Header = (e.Row.GetIndex() + 1).ToString(); } -} \ No newline at end of file +} diff --git a/HandyControl/Controls/TransitioningContentControl.cs b/HandyControl/Controls/TransitioningContentControl.cs index 574a0525..64c0cf75 100644 --- a/HandyControl/Controls/TransitioningContentControl.cs +++ b/HandyControl/Controls/TransitioningContentControl.cs @@ -119,7 +119,7 @@ namespace HandyControl.Controls private Storyboard GetStoryboard(string newTransition) { - var presentationGroup = VisualStates.TryGetVisualStateGroup(this, PresentationGroup); + var presentationGroup = VisualHelper.TryGetVisualStateGroup(this, PresentationGroup); Storyboard newStoryboard = null; if (presentationGroup != null) newStoryboard = presentationGroup.States @@ -218,7 +218,7 @@ namespace HandyControl.Controls var newStoryboard = source.GetStoryboard(newTransition); if (newStoryboard == null) - if (VisualStates.TryGetVisualStateGroup(source, PresentationGroup) == null) + if (VisualHelper.TryGetVisualStateGroup(source, PresentationGroup) == null) { source.CurrentTransition = null; } diff --git a/HandyControl/HandyControl.csproj b/HandyControl/HandyControl.csproj index d9df1754..ef651cad 100644 --- a/HandyControl/HandyControl.csproj +++ b/HandyControl/HandyControl.csproj @@ -204,7 +204,7 @@ - + diff --git a/HandyControl/Themes/Styles/DataGrid.xaml b/HandyControl/Themes/Styles/DataGrid.xaml index 5404385c..1e024b94 100644 --- a/HandyControl/Themes/Styles/DataGrid.xaml +++ b/HandyControl/Themes/Styles/DataGrid.xaml @@ -82,14 +82,15 @@ - - - - - + + + + + +