【发布时间】:2012-03-13 00:46:53
【问题描述】:
我想在(只读)DataGrid 中显示一种富文本。它应包括基本样式(粗体、斜体)和超链接,即网格单元应采用以下内容:
这里和也在这里你可以找到stuff并将其渲染为 html。当然,数据来自数据库,所以一切都应该是可绑定的。
在尝试修补适当的模板或样式后,我放弃并尝试制作自定义控件。控制相当简单:
public class SmartTextBlock : TextBlock
{
public static readonly DependencyProperty HtmlProperty = DependencyProperty.Register("Html", typeof(string), typeof(SmartTextBlock), new UIPropertyMetadata(null, new PropertyChangedCallback(OnHtmlChanged)));
public string Html
{
get { return GetValue(HtmlProperty) as string; }
set { SetValue(HtmlProperty, value); }
}
static void OnHtmlChanged(DependencyObject target, DependencyPropertyChangedEventArgs e) {
SmartTextBlock control = target as SmartTextBlock;
control.createParts(e.NewValue as string);
}
void createParts(string text) {
Inlines.Clear();
/* parse text and add some Inlines */
}
}
进展顺利,控制显示良好。它被顺利添加到网格中:
<DataGridTemplateColumn Width="*" Header="Line">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<controls:SmartTextBlock Html="{Binding Path=Html}" TextWrapping="Wrap" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
问题是当我尝试调整或滚动网格时。然后整个窗口开始闪烁并且应用程序挂起!这不会每次都发生,但如果您尝试足够长的时间,它肯定会开始。
任何想法可能是什么原因,我忽略了哪个明显的错误?
【问题讨论】:
-
作为建议 - 尝试为自己编写的方法设置断点,尝试调整大小并查看调用了哪个方法。
-
睡了一会儿后,我有了一些想法——第一个是将列宽从 Width="*" 更改为 Width="220"。好消息是它不再闪烁(渲染中的无限循环)和挂起。所以它必须与 DataGrid 的内部布局逻辑有关。此外,尽管 DataGrid 有 HeadersVisibility="Column" 一些行(随机)会突然获得行标题!看来我的问题来自对 DataGrid 的有缺陷的(微不足道的)理解。将尝试用 ListView 替换它。