使用DataGridTemplateColumn时WPF数据网格的性能

时间:2013-01-03 06:03:46

标签: c# wpf datagrid datagridtemplatecolumn

我正在使用DataGrid显示仓库占用情况(占用显示图像带框,未占用 - 显示空图像)。
在DataGrid中,我使用DataGridTemplateColumn覆盖图像。 我的主要表格XAML代码:

<xctk:BusyIndicator Name="ctrlBusy" IsBusy="False" BusyContent="Generating Maps..." >
<Grid>
    <StackPanel>
        <Button Name="btnClick" Grid.Row="0" Click="Button_Click_1" Height="44" VerticalAlignment="Top" 
        HorizontalAlignment="Left" Width="114" Panel.ZIndex="4" Margin="6,3,0,0">Click</Button>

        <StackPanel Orientation="Vertical" Grid.Row="1">
            <TextBlock  Background="SkyBlue" Height="50">

            </TextBlock>
            <DataGrid GridLinesVisibility="None" Background="SkyBlue" 
          BorderBrush="Transparent" IsReadOnly="True" ItemsSource="{Binding}" 
          AutoGenerateColumns="True" AutoGeneratingColumn="dgvMap_AutoGeneratingColumn"   
          CanUserAddRows="False" CanUserSortColumns="true" CanUserDeleteRows="False"  
          HeadersVisibility="Row" Name="dgvMap" SelectionMode="Single" 
          Panel.ZIndex="0" Margin="0,0,0,0" VirtualizingStackPanel.VirtualizationMode="Standard">
                <!--for removing the blue color bkground default for row selection-->
                <DataGrid.Resources>
                    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" 
           Color="Transparent"/>
                </DataGrid.Resources>
            </DataGrid>
            <TextBlock  Background="SkyBlue" Height="50">

            </TextBlock>
        </StackPanel>
    </StackPanel>
</Grid>


DataGrid的Datatemplate:

<DataTemplate x:Key="MyDataTemplate" DataType="DataRowView">
    <Grid Background="Transparent">
        <Image Tag="{Binding}" Name="Layer0" Margin="0,0,0,0"  Panel.ZIndex="1"  
               Width="50" Height="50"   ToolTipService.HasDropShadow="True" ToolTipService.ShowDuration="20000" ToolTipService.InitialShowDelay="200" >
            <Image.ToolTip>
                <StackPanel>
                    <Label FontWeight="Bold" Background="Blue" Foreground="White" Content="{Binding}" />
                    <TextBlock Padding="10" TextWrapping="WrapWithOverflow" Width="200">
                        This coil is located in this location. Yard Name is FG. Zone is Dispatch Area.
                    </TextBlock>
                    <Line Stroke="Black" StrokeThickness="1" X2="200" />
                    <StackPanel Orientation="Horizontal">
                        <Label FontWeight="Bold">Report to admin in case of coil location mismatch</Label>
                    </StackPanel>
                </StackPanel>
            </Image.ToolTip>
            <Image.Resources>
                <Style TargetType="{x:Type Image}">
                    <Setter Property="Source" Value="{Binding Converter={StaticResource IntToImageConverter}, ConverterParameter = Layer0}" />
                    <Style.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <!-- Hover image -->
                            <Setter Property="Cursor" Value="Hand"/>
                            <Setter Property="Source" Value="C:\Users\Coil3.png"/>
                            <!--<Setter Property="Source" Value="{Binding  Converter={StaticResource HoverImage}}"/>-->
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </Image.Resources>
        </Image>
    </Grid>
</DataTemplate>  

主要表格代码 - 背后:

   private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        btnClick.Content = "Data Loaded";
        Stopwatch sw = new Stopwatch();
        DataTable dt = dbLayer.tblSaddleSelectAll();
        sw.Start();
        dgvMap.ItemsSource = dt.DefaultView;
        sw.Stop();
        btnClick.Content = sw.ElapsedMilliseconds.ToString();

    }



  private void dgvMap_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
    {
        if (e.PropertyName == "row")
        {
            e.Column.Visibility = System.Windows.Visibility.Hidden;
        }
        var column = new DataRowColumn(e.PropertyName);
        column.Header = e.Column.Header;
        column.CellTemplate = (DataTemplate)Resources["MyDataTemplate"];

        e.Column = column;
    }  

DataGrid的ValueConverter:

public class BoolToImageConverter : IValueConverter
{

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        ImageSource result = null;
        var intValue = value.ToString();
        switch (parameter.ToString())
        {
            case "Layer1":
                if (intValue.ToUpper().Contains("EMPTY"))
                {
                    result = null;
                }
                else
                {
                    result = new BitmapImage(new Uri(@"C:\Users\Images\Box3.png"));
                }
                return result;
            default:
                if (intValue.ToUpper().Contains("EMPTY"))
                {
                    //result = null;
                    result = new BitmapImage(new Uri(@"C:\Users\Images\Box1.png"));
                }
                else
                {

                    result = new BitmapImage(new Uri(@"C:\Users\Images\Box2.png"));
                }
                return result;
        }

    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}  

自定义DatagridTemplateColumn:

public class DataRowColumn : DataGridTemplateColumn
{
    public DataRowColumn(string column) { ColumnName = column; }
    public string ColumnName { get; private set; }
    protected override FrameworkElement GenerateElement(DataGridCell cell, object dataItem)
    {
        var row = (DataRowView)dataItem;
        var item = row[ColumnName];
        cell.DataContext = item;
        var element = base.GenerateElement(cell, item);
        return element;
    }
}

数据库中的数据将如下所示:
enter image description here

我将从数据库中加载最多250列,20行 我的问题是:
我一直用秒表来检查加载DataGrid的时间。它显示的值小于250毫秒。但实际上它需要花费太多时间才能显示,并且在4-6秒内UI被挂起。为什么要挂?如何克服它?
2.将DataTable的DefaultView附加到DataGrid是更好的方法吗? 3. datagrid是显示给定范围数据的地图类型布局的最佳方式吗?我需要通过一些工具提示说明来显示在场和缺席 4.在DataGrid中我是否遗漏了任何(属性)以提高性能。

1 个答案:

答案 0 :(得分:1)

是的,您还可以采取一些其他措施来提高性能。事实上,我之前已回答过这个问题。

请看这个问题和我的答案。

Improve DataGrid Performance