Merge pull request #15 from ghost1372/patch-4
add showRowNumber property to DataGrid
This commit is contained in:
6 files changed
+116
-30
No files matched your search
@@ -1,6 +1,8 @@
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Windows;
|
using System.Windows;
|
||||||
using System.Windows.Controls;
|
using System.Windows.Controls;
|
||||||
|
using System.Windows.Controls.Primitives;
|
||||||
|
using HandyControl.Data;
|
||||||
|
|
||||||
namespace HandyControl.Controls
|
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -119,7 +119,7 @@ namespace HandyControl.Controls
|
|||||||
|
|
||||||
private Storyboard GetStoryboard(string newTransition)
|
private Storyboard GetStoryboard(string newTransition)
|
||||||
{
|
{
|
||||||
var presentationGroup = VisualStates.TryGetVisualStateGroup(this, PresentationGroup);
|
var presentationGroup = VisualHelper.TryGetVisualStateGroup(this, PresentationGroup);
|
||||||
Storyboard newStoryboard = null;
|
Storyboard newStoryboard = null;
|
||||||
if (presentationGroup != null)
|
if (presentationGroup != null)
|
||||||
newStoryboard = presentationGroup.States
|
newStoryboard = presentationGroup.States
|
||||||
@@ -218,7 +218,7 @@ namespace HandyControl.Controls
|
|||||||
var newStoryboard = source.GetStoryboard(newTransition);
|
var newStoryboard = source.GetStoryboard(newTransition);
|
||||||
|
|
||||||
if (newStoryboard == null)
|
if (newStoryboard == null)
|
||||||
if (VisualStates.TryGetVisualStateGroup(source, PresentationGroup) == null)
|
if (VisualHelper.TryGetVisualStateGroup(source, PresentationGroup) == null)
|
||||||
{
|
{
|
||||||
source.CurrentTransition = null;
|
source.CurrentTransition = null;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -204,7 +204,7 @@
|
|||||||
<Compile Include="Tools\RegularPatterns.cs" />
|
<Compile Include="Tools\RegularPatterns.cs" />
|
||||||
<Compile Include="Tools\ResourceHelper.cs" />
|
<Compile Include="Tools\ResourceHelper.cs" />
|
||||||
<Compile Include="Tools\ValidateHelper.cs" />
|
<Compile Include="Tools\ValidateHelper.cs" />
|
||||||
<Compile Include="Tools\VisualStates.cs" />
|
<Compile Include="Tools\VisualHelper.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Page Include="Themes\Basic\Basic.xaml">
|
<Page Include="Themes\Basic\Basic.xaml">
|
||||||
|
|||||||
@@ -82,14 +82,15 @@
|
|||||||
<DataGridDetailsPresenter Margin="0,6" Grid.Column="1" Grid.Row="1" SelectiveScrollingGrid.SelectiveScrollingOrientation="{Binding AreRowDetailsFrozen, ConverterParameter={x:Static SelectiveScrollingOrientation.Vertical}, Converter={x:Static DataGrid.RowDetailsScrollingConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" Visibility="{TemplateBinding DetailsVisibility}"/>
|
<DataGridDetailsPresenter Margin="0,6" Grid.Column="1" Grid.Row="1" SelectiveScrollingGrid.SelectiveScrollingOrientation="{Binding AreRowDetailsFrozen, ConverterParameter={x:Static SelectiveScrollingOrientation.Vertical}, Converter={x:Static DataGrid.RowDetailsScrollingConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" Visibility="{TemplateBinding DetailsVisibility}"/>
|
||||||
<DataGridRowHeader Grid.Row="0" Grid.Column="0" SelectiveScrollingGrid.SelectiveScrollingOrientation="Vertical" Visibility="{Binding HeadersVisibility, ConverterParameter={x:Static DataGridHeadersVisibility.Row}, Converter={x:Static DataGrid.HeadersVisibilityConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>
|
<DataGridRowHeader Grid.Row="0" Grid.Column="0" SelectiveScrollingGrid.SelectiveScrollingOrientation="Vertical" Visibility="{Binding HeadersVisibility, ConverterParameter={x:Static DataGridHeadersVisibility.Row}, Converter={x:Static DataGrid.HeadersVisibilityConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>
|
||||||
</SelectiveScrollingGrid>
|
</SelectiveScrollingGrid>
|
||||||
<ControlTemplate.Triggers>
|
|
||||||
<Trigger Property="IsSelected" Value="True">
|
|
||||||
<Setter Property="Background" Value="{DynamicResource PrimaryBrush}"/>
|
|
||||||
</Trigger>
|
|
||||||
</ControlTemplate.Triggers>
|
|
||||||
</ControlTemplate>
|
</ControlTemplate>
|
||||||
</Setter.Value>
|
</Setter.Value>
|
||||||
</Setter>
|
</Setter>
|
||||||
|
<Style.Triggers>
|
||||||
|
<Trigger Property="IsSelected" Value="True">
|
||||||
|
<Setter Property="Background" Value="{DynamicResource PrimaryBrush}"/>
|
||||||
|
<Setter Property="TextElement.Foreground" Value="White"/>
|
||||||
|
</Trigger>
|
||||||
|
</Style.Triggers>
|
||||||
</Style>
|
</Style>
|
||||||
<Style x:Key="ColumnHeaderGripperStyle" TargetType="Thumb">
|
<Style x:Key="ColumnHeaderGripperStyle" TargetType="Thumb">
|
||||||
<Setter Property="Width" Value="8"/>
|
<Setter Property="Width" Value="8"/>
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ using System.Windows.Media;
|
|||||||
|
|
||||||
namespace HandyControl.Tools
|
namespace HandyControl.Tools
|
||||||
{
|
{
|
||||||
public class VisualStates
|
internal class VisualHelper
|
||||||
{
|
{
|
||||||
public static VisualStateGroup TryGetVisualStateGroup(DependencyObject dependencyObject, string groupName)
|
public static VisualStateGroup TryGetVisualStateGroup(DependencyObject dependencyObject, string groupName)
|
||||||
{
|
{
|
||||||
@@ -6,26 +6,47 @@
|
|||||||
xmlns:langs="clr-namespace:HandyControlDemo.Properties.Langs"
|
xmlns:langs="clr-namespace:HandyControlDemo.Properties.Langs"
|
||||||
xmlns:controls="clr-namespace:HandyControl.Controls;assembly=HandyControl">
|
xmlns:controls="clr-namespace:HandyControl.Controls;assembly=HandyControl">
|
||||||
<controls:TransitioningContentControl>
|
<controls:TransitioningContentControl>
|
||||||
<DataGrid HeadersVisibility="All" RowHeaderWidth="60" AutoGenerateColumns="False" ItemsSource="{Binding DataList}">
|
<TabControl Style="{StaticResource TabControlInLine}">
|
||||||
<DataGrid.RowHeaderTemplate>
|
<TabItem Header="Common">
|
||||||
<DataTemplate>
|
<DataGrid HeadersVisibility="All" RowHeaderWidth="60" AutoGenerateColumns="False" ItemsSource="{Binding DataList}">
|
||||||
<CheckBox IsChecked="{Binding IsSelected,RelativeSource={RelativeSource FindAncestor,AncestorType=DataGridRow}}"/>
|
<DataGrid.RowHeaderTemplate>
|
||||||
</DataTemplate>
|
|
||||||
</DataGrid.RowHeaderTemplate>
|
|
||||||
<DataGrid.Columns>
|
|
||||||
<DataGridTextColumn IsReadOnly="True" Width="80" CanUserResize="False" Binding="{Binding Index}" Header="{x:Static langs:Lang.Index}"/>
|
|
||||||
<DataGridTemplateColumn Width="50" CanUserResize="False">
|
|
||||||
<DataGridTemplateColumn.CellTemplate>
|
|
||||||
<DataTemplate>
|
<DataTemplate>
|
||||||
<Image Source="{Binding ImgPath}" Width="32" Height="32" Stretch="Uniform"/>
|
<CheckBox IsChecked="{Binding IsSelected,RelativeSource={RelativeSource FindAncestor,AncestorType=DataGridRow}}"/>
|
||||||
</DataTemplate>
|
</DataTemplate>
|
||||||
</DataGridTemplateColumn.CellTemplate>
|
</DataGrid.RowHeaderTemplate>
|
||||||
</DataGridTemplateColumn>
|
<DataGrid.Columns>
|
||||||
<DataGridTextColumn Width="1*" Binding="{Binding Name}" Header="{x:Static langs:Lang.Name}"/>
|
<DataGridTextColumn IsReadOnly="True" Width="80" CanUserResize="False" Binding="{Binding Index}" Header="{x:Static langs:Lang.Index}"/>
|
||||||
<DataGridCheckBoxColumn Width="100" CanUserResize="False" Binding="{Binding IsSelected}" Header="{x:Static langs:Lang.Selected}"/>
|
<DataGridTemplateColumn Width="50" CanUserResize="False">
|
||||||
<DataGridComboBoxColumn ItemsSource="{Binding Source={StaticResource DemoTypes}}" Width="100" CanUserResize="False" SelectedValueBinding="{Binding Type}" Header="{x:Static langs:Lang.Type}"/>
|
<DataGridTemplateColumn.CellTemplate>
|
||||||
<DataGridTextColumn Width="1*" Binding="{Binding Remark}" Header="{x:Static langs:Lang.Remark}"/>
|
<DataTemplate>
|
||||||
</DataGrid.Columns>
|
<Image Source="{Binding ImgPath}" Width="32" Height="32" Stretch="Uniform"/>
|
||||||
</DataGrid>
|
</DataTemplate>
|
||||||
|
</DataGridTemplateColumn.CellTemplate>
|
||||||
|
</DataGridTemplateColumn>
|
||||||
|
<DataGridTextColumn Width="1*" Binding="{Binding Name}" Header="{x:Static langs:Lang.Name}"/>
|
||||||
|
<DataGridCheckBoxColumn Width="100" CanUserResize="False" Binding="{Binding IsSelected}" Header="{x:Static langs:Lang.Selected}"/>
|
||||||
|
<DataGridComboBoxColumn ItemsSource="{Binding Source={StaticResource DemoTypes}}" Width="100" CanUserResize="False" SelectedValueBinding="{Binding Type}" Header="{x:Static langs:Lang.Type}"/>
|
||||||
|
<DataGridTextColumn Width="1*" Binding="{Binding Remark}" Header="{x:Static langs:Lang.Remark}"/>
|
||||||
|
</DataGrid.Columns>
|
||||||
|
</DataGrid>
|
||||||
|
</TabItem>
|
||||||
|
<TabItem Header="ShowRowNumber">
|
||||||
|
<DataGrid HeadersVisibility="All" RowHeaderWidth="60" AutoGenerateColumns="False" ItemsSource="{Binding DataList}" controls:DataGridAttach.ShowRowNumber="True">
|
||||||
|
<DataGrid.Columns>
|
||||||
|
<DataGridTemplateColumn Width="50" CanUserResize="False">
|
||||||
|
<DataGridTemplateColumn.CellTemplate>
|
||||||
|
<DataTemplate>
|
||||||
|
<Image Source="{Binding ImgPath}" Width="32" Height="32" Stretch="Uniform"/>
|
||||||
|
</DataTemplate>
|
||||||
|
</DataGridTemplateColumn.CellTemplate>
|
||||||
|
</DataGridTemplateColumn>
|
||||||
|
<DataGridTextColumn Width="1*" Binding="{Binding Name}" Header="{x:Static langs:Lang.Name}"/>
|
||||||
|
<DataGridCheckBoxColumn Width="100" CanUserResize="False" Binding="{Binding IsSelected}" Header="{x:Static langs:Lang.Selected}"/>
|
||||||
|
<DataGridComboBoxColumn ItemsSource="{Binding Source={StaticResource DemoTypes}}" Width="100" CanUserResize="False" SelectedValueBinding="{Binding Type}" Header="{x:Static langs:Lang.Type}"/>
|
||||||
|
<DataGridTextColumn Width="1*" Binding="{Binding Remark}" Header="{x:Static langs:Lang.Remark}"/>
|
||||||
|
</DataGrid.Columns>
|
||||||
|
</DataGrid>
|
||||||
|
</TabItem>
|
||||||
|
</TabControl>
|
||||||
</controls:TransitioningContentControl>
|
</controls:TransitioningContentControl>
|
||||||
</UserControl>
|
</UserControl>
|
||||||
Reference in new issue
Block a user