Merge pull request #15 from ghost1372/patch-4

add showRowNumber property to DataGrid
This commit is contained in:
NaBian 2018-11-22 09:52:11 +08:00 committed by GitHub
commit 5ac75d4d36
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 116 additions and 30 deletions

View File

@ -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();
}
}
}

View File

@ -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;
}

View File

@ -204,7 +204,7 @@
<Compile Include="Tools\RegularPatterns.cs" />
<Compile Include="Tools\ResourceHelper.cs" />
<Compile Include="Tools\ValidateHelper.cs" />
<Compile Include="Tools\VisualStates.cs" />
<Compile Include="Tools\VisualHelper.cs" />
</ItemGroup>
<ItemGroup>
<Page Include="Themes\Basic\Basic.xaml">

View File

@ -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}"/>
<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>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="{DynamicResource PrimaryBrush}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</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 x:Key="ColumnHeaderGripperStyle" TargetType="Thumb">
<Setter Property="Width" Value="8"/>

View File

@ -4,7 +4,7 @@ using System.Windows.Media;
namespace HandyControl.Tools
{
public class VisualStates
internal class VisualHelper
{
public static VisualStateGroup TryGetVisualStateGroup(DependencyObject dependencyObject, string groupName)
{

View File

@ -6,26 +6,47 @@
xmlns:langs="clr-namespace:HandyControlDemo.Properties.Langs"
xmlns:controls="clr-namespace:HandyControl.Controls;assembly=HandyControl">
<controls:TransitioningContentControl>
<DataGrid HeadersVisibility="All" RowHeaderWidth="60" AutoGenerateColumns="False" ItemsSource="{Binding DataList}">
<DataGrid.RowHeaderTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsSelected,RelativeSource={RelativeSource FindAncestor,AncestorType=DataGridRow}}"/>
</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>
<TabControl Style="{StaticResource TabControlInLine}">
<TabItem Header="Common">
<DataGrid HeadersVisibility="All" RowHeaderWidth="60" AutoGenerateColumns="False" ItemsSource="{Binding DataList}">
<DataGrid.RowHeaderTemplate>
<DataTemplate>
<Image Source="{Binding ImgPath}" Width="32" Height="32" Stretch="Uniform"/>
<CheckBox IsChecked="{Binding IsSelected,RelativeSource={RelativeSource FindAncestor,AncestorType=DataGridRow}}"/>
</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>
</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>
<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>
<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>
</UserControl>