C# 使用DataGridTemplateColumn时WPF datagrid的性能
我正在使用C# 使用DataGridTemplateColumn时WPF datagrid的性能,c#,wpf,datagrid,datagridtemplatecolumn,C#,Wpf,Datagrid,Datagridtemplatecolumn,我正在使用DataGrid显示仓库占用情况(用盒子显示占用的图像,而不是显示占用的空图像)。 在DataGrid中,我使用DataGridTemplateColumn覆盖图像。 我的主要表单XAML代码: <xctk:BusyIndicator Name="ctrlBusy" IsBusy="False" BusyContent="Generating Maps..." > <Grid> <StackPanel> <Button
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>
数据网格的值转换器:
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();
}
}
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;
}
}
自定义DatagridTemplateColumn:
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();
}
}
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;
}
}
来自数据库的数据如下:我将从数据库中最多加载250列20行。
我的问题是:
1.我用来检查加载DataGrid所用时间的秒表。显示值小于250ms。但在现实中,它花费了太多的时间,在4-6秒钟内,用户界面被挂起。为什么要绞刑?如何克服它?
2.在性能方面,将DataTable的DefaultView附加到DataGrid是更好的方法吗??
3.datagrid是显示给定数据范围内地图类型布局的最佳方式吗?我需要用一些工具提示说明来显示有无。
4.在DataGrid中,我是否遗漏了一些(属性)来提高性能。是的,您还可以做一些其他事情来提高性能。事实上,我以前回答过一个问题 请看这个问题和我的答案
是的,您还可以做一些其他事情来提高性能。事实上,我以前回答过一个问题 请看这个问题和我的答案