为WPF版的GridControl控件添加行序号功能

废话不多数,先上效果图和代码:

为WPF版的GridControl控件添加行序号功能

包装GridControl控件

cs

using DevExpress.Xpf.Data;
using DevExpress.Xpf.Grid;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes; namespace WPFDemo
{
/// <summary>
/// PageGridControl.xaml 的交互逻辑
/// </summary>
public partial class UCGridControl : GridControl
{
private Visibility showOrderNum = Visibility.Collapsed;
/// <summary>
/// 是否显示序号
/// </summary>
public Visibility ShowOrderNum
{
get
{
return showOrderNum;
}
set
{
showOrderNum = value;
if (value == Visibility.Visible)
{
tbv.IndicatorWidth = ;
var dt = this.FindResource("DataTemplate1") as DataTemplate;
if (dt != null)
tbv.RowIndicatorContentTemplate = dt;
}
}
}
/// <summary>
/// 序号列宽度
/// </summary>
public double OrderNumWidth
{
get
{
return tbv.IndicatorWidth;
}
set
{
tbv.IndicatorWidth = value;
}
} public UCGridControl()
{
InitializeComponent(); }
} /// <summary>
/// 行序号转换器
/// </summary>
public class RowOrderNumConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var rowHandle = value as RowHandle;
if (rowHandle != null)
{
int num = rowHandle.Value;
return (num + );
}
return "";
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return ;
}
}
}

xmal

<dxg:GridControl x:Class="WPFDemo.UCGridControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WPFDemo"
xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"
xmlns:dxgt="http://schemas.devexpress.com/winfx/2008/xaml/grid/themekeys"
xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors"
x:Name="uc"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<dxg:GridControl.Resources>
<local:RowOrderNumConverter x:Key="RowOrderNumConverter"/>
<DataTemplate x:Key="DataTemplate1">
<Border x:Name="errorIndicatorBorder" Background="Transparent">
<Grid>
<TextBlock x:Name="txtOrderNum" TextAlignment="Center" Text="{Binding RowHandle,Converter={StaticResource RowOrderNumConverter}}" Visibility="{Binding ShowOrderNum, ElementName=uc}" />
<ContentPresenter x:Name="iconPresenter">
<ContentPresenter.ContentTemplate>
<DataTemplate>
<Grid/>
</DataTemplate>
</ContentPresenter.ContentTemplate>
</ContentPresenter>
</Grid>
</Border>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding IndicatorState}" Value="Focused">
<Setter Property="ContentTemplate" TargetName="iconPresenter" Value="{DynamicResource {dxgt:RowIndicatorThemeKey ResourceKey=FocusedIconTemplate}}"/>
<Setter Property="Visibility" TargetName="txtOrderNum" Value="Collapsed"/>
</DataTrigger>
<DataTrigger Binding="{Binding IndicatorState}" Value="Changed">
<Setter Property="ContentTemplate" TargetName="iconPresenter" Value="{DynamicResource {dxgt:RowIndicatorThemeKey ResourceKey=ChangedIconTemplate}}"/>
<Setter Property="Visibility" TargetName="txtOrderNum" Value="Collapsed"/>
</DataTrigger>
<DataTrigger Binding="{Binding IndicatorState}" Value="NewItemRow">
<Setter Property="ContentTemplate" TargetName="iconPresenter" Value="{DynamicResource {dxgt:RowIndicatorThemeKey ResourceKey=NewItemRowIconTemplate}}"/>
<Setter Property="Visibility" TargetName="txtOrderNum" Value="Collapsed"/>
</DataTrigger>
<DataTrigger Binding="{Binding IndicatorState}" Value="Editing">
<Setter Property="ContentTemplate" TargetName="iconPresenter" Value="{DynamicResource {dxgt:RowIndicatorThemeKey ResourceKey=EditingIconTemplate}}"/>
<Setter Property="Visibility" TargetName="txtOrderNum" Value="Collapsed"/>
</DataTrigger>
<DataTrigger Binding="{Binding IndicatorState}" Value="Error">
<Setter Property="ContentTemplate" TargetName="iconPresenter" Value="{DynamicResource {dxgt:RowIndicatorThemeKey ResourceKey=ErrorIconTemplate}}"/>
<Setter Property="Visibility" TargetName="txtOrderNum" Value="Collapsed"/>
</DataTrigger>
<DataTrigger Binding="{Binding IndicatorState}" Value="FocusedError">
<Setter Property="ContentTemplate" TargetName="iconPresenter" Value="{DynamicResource {dxgt:RowIndicatorThemeKey ResourceKey=FocusedErrorIconTemplate}}"/>
<Setter Property="Visibility" TargetName="txtOrderNum" Value="Collapsed"/>
</DataTrigger>
<DataTrigger Binding="{Binding IndicatorState}" Value="AutoFilterRow">
<Setter Property="ContentTemplate" TargetName="iconPresenter" Value="{DynamicResource {dxgt:RowIndicatorThemeKey ResourceKey=AutoFilterRowIconTemplate}}"/>
<Setter Property="Visibility" TargetName="txtOrderNum" Value="Collapsed"/>
</DataTrigger>
<DataTrigger Binding="{Binding (dxe:BaseEdit.HasValidationError)}" Value="True">
<Setter Property="ToolTip" TargetName="errorIndicatorBorder">
<Setter.Value>
<DockPanel>
<dxe:ErrorControl Content="{Binding (dxe:BaseEdit.ValidationError)}"/>
<ContentPresenter Content="{Binding (dxe:BaseEdit.ValidationError).ErrorContent}" VerticalAlignment="Center"/>
</DockPanel>
</Setter.Value>
</Setter>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</dxg:GridControl.Resources>
<dxg:GridControl.View>
<dxg:TableView x:Name="tbv"/>
</dxg:GridControl.View>
</dxg:GridControl>

使用

        <local:UCGridControl x:Name="gc">
<dxg:GridControl.Columns>
<dxg:GridColumn FieldName="Name" Header="Name"/>
</dxg:GridControl.Columns>
</local:UCGridControl> var list = new List<object>();
for (int i = 0; i <; i++)
{
list.Add(new { Name="test"+(i+1)});
}
gc.ItemsSource = list;
gc.ShowOrderNum = Visibility.Visible;

主要代码是重新设置TableView的行标题模板:

<DataTemplate x:Key="DataTemplate1">
<Border x:Name="errorIndicatorBorder" Background="Transparent">
<Grid>

<TextBlock x:Name="txtOrderNum" TextAlignment="Center" Text="{Binding RowHandle,Converter={StaticResource RowOrderNumConverter}}" Visibility="{Binding ShowOrderNum, ElementName=uc}" />

......
var dt = this.FindResource("DataTemplate1") as DataTemplate;
if (dt != null)
tbv.RowIndicatorContentTemplate = dt;

绑定的RowHandle属性包含行索引,使用个转换器加1就可以转换成序号了。

源代码路径:https://github.com/yangxinwen/GitHubDemo/tree/master/WPFDemo

上一篇:WebSocket数据包协议详解


下一篇:python成长之路【第十三篇】:Python操作MySQL之pymysql