如何动态风格WPF DataGridCell [英] How to style a WPF DataGridCell dynamically
问题描述
我与的ItemsSource DataGrid中定义如下:
I have a DataGrid with the itemsSource defined as follow:
dg.ItemsSource = new ObservableCollection<myRow>
...
public class myRow : Collection<myDataGridCell> { ... }
...
public interface ImyDataGridCell
{
Brush Background { get; set; }
Brush Foreground { get; set; }
}
然后我有类为每种类型的列/单元格的:
and then I have classes for each type of column/cell:
public class myTextBoxColumn : DataGridTextColumn {...}
public class myTextBoxCell : TextBox, ImyDataGridCell {...}
然后我设置的每个列的CellStyle是这样的:
then I set each column's CellStyle like this:
在每一列的构造函数:
string source = String.Format(CultureInfo.InvariantCulture, "[{0}].", dataGrid.Columns.Count);
// I set the "source" string to the column's index in the dataGrid's Columns list between [] to get an index in my binding below.
CellStyle = new Style(typeof(DataGridCell));
CellStyle.Setters.Add(new Setter(DataGridCell.BackgroundProperty, new Binding(source + "Background")));
这让我实际DataGridCell的背景属性绑定到我的手机的再presentation的背景属性,从而修改单元格背景随时随地我想很容易。
this allows me to bind the actual DataGridCell's Background property to the Background property of my cell's representation, and thus to modify a cell's background anytime I want easily.
现在我的主要问题是,做事情的这种方式减慢像地狱DataGrid中...
我身边有15性质我绑定的每一个细胞,当我告诉100 COLS×20行需要超过一秒,显示在DataGrid,然后一秒钟刷新它,当我水平滚动(我的屏幕只能允许20 COLS的时间,我也为DataGrid启用虚拟化)。
now my main issue is that this way of doing things slows the dataGrid like hell... I have around 15 properties I bind for each cell, and when I show 100 cols x 20 rows it takes more than a second to show the dataGrid and then about a second to refresh it when I scroll horizontally (my screen can only allow for 20 cols at a time, and I have Virtualization enabled for the dataGrid).
如果我摆脱了造型,响应时间仍超过我想,但我可以用它做什么。
If I get rid of the styling, the response time is still more than I'd like, but I could do with it.
那么,有没有更好的方式来做到这一点?
So is there a better way to do this?
我也试过在XAML这样的风格,但它似乎没有与列的虚拟化,即应付得好:我会设置一个单元格背景的绿色,然后滚动整个页面右侧,并且在同一个最终的单元格因为我已经漆成绿色的单元位置得到绿色的事件,如果它应该是红色:值不更新,直到我当前行移动到包含该单元格的行...
再加上,它似乎并没有提高性能,在所有...
I also tried doing this style in Xaml, but It did not seem to cope well with column's virtualization, i.e.: I would set a cell's background green, then scroll a whole page right, and the cell that ends up in the same position as the cell I had painted green gets the green color event if it should be red: the value is not updated untill I move the current row to the row containing the cell... plus, it did not seem to improve performance at all...
感谢,如果您有任何形式的,虽然对此事/尖/ previous经验分享,我很愿意尝试一切可能让这个该死的数据网格加载速度更快...
thanks for sharing if you have any kind of though/tip/previous experience on the matter, I'm willing to try about everything that could make this damn dataGrid load faster...
编辑:常规效果我想实现:
- 与动态列(该列的数目和类型一个DataGrid在运行时只知道
- 在任何时候,我可以改变一个单细胞的任何样式属性:字体(系列,大小,款式,重量,如果应用饰),前景,背景,textAlignment如有等等...
这正是我要实现的。
知道我发现,与列虚拟化,你不能操纵真实dataGridCells,因为他们可能无法显示,但(虚拟化),然后你失去的属性值的变化。于是我就为这个黑客:我在实际dataGridCell的风格每一个属性绑定到逻辑一,我修改逻辑之一。但是,这是slooooow。
this is just what I have to achieve. know I found that with column virtualization on, you cannot manipulate the REAL dataGridCells, as they might not be shown yet (virtualized), and then you loose the property value's change. So I went for this "hack": I bind every single property in the actual dataGridCell's style to the "logical" one, and I modify the logical one. But this is slooooow.
希望我设法解释自己好一点。
hope I managed to explain myself a little better.
推荐答案
看到你的速度慢的XAML任何机会呢?我本来以为有datatrigger做这不会是太可怕了(也是数据网格的哪个版本是您使用的.NET 4.0和WPF工具包的版本是不同的)
Any chance of seeing your slow Xaml? I would have thought that doing this with a datatrigger wouldn't be too awful (also which version of the data grid are you using as the .net 4.0 and the WPF Toolkit versions are different)
我做过这样的东西重新着色为选定的项目和它似乎没有过慢(这不是正确的解决方案,但我想更详细一点之前,我再多说了):
I've done stuff like this to recolor for selected items and it didn't seem too slow (this isn't the right solution but I'd like a little more detail before I say any more):
<Style TargetType="DataGrid">
<Setter Property="CellStyle">
<Setter.Value>
<Style TargetType="DataGridCell">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="{StaticResource SelectedBackgroundBrush}" />
<Setter Property="BorderBrush" Value="{x:Null}" />
<Setter Property="Foreground" Value="White" />
</Trigger>
</Style.Triggers>
</Style>
</Setter.Value>
</Setter>
</Style>
这篇关于如何动态风格WPF DataGridCell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!