简体   繁体   English

何时呈现自定义 WPF TextBlock?

[英]When to render custom WPF TextBlock?

When should I be building Inlines in a TextBlock?我应该什么时候在 TextBlock 中构建内联? I have a TextBlock-derived class that, when given text in a certain field, call it MyText, converts the text into a set of inlines when MyText has changed.我有一个 TextBlock 派生的 class,当在某个字段中给定文本时,将其称为 MyText,当 MyText 发生更改时,将文本转换为一组内联。

Whenever MyText changes, I clear the Inlines and build them, colorizing each word as needed.每当 MyText 发生变化时,我都会清除内联并构建它们,并根据需要为每个单词着色。 For this example, consider:对于此示例,请考虑:

private void MyTextBlock_MyTextChanged(object sender, EventArgs e)
{
    Inlines.Clear();
    if (!string.IsNullOrEmpty(this.MyText))
    {
        var run = new Run();
        run.Foreground = Brushes.DarkRed;
        run.Text = this.MyText;
        Inlines.Add(run);
    }
}

This has worked very well.这非常有效。 However, recently we placed the Control into a DataGrid, and some strange things have started happening.然而,最近我们将控件放入一个DataGrid,并且开始发生一些奇怪的事情。 Apparently the DataGrid swaps out the context and for the most part this works.显然,DataGrid 交换了上下文,并且在大多数情况下这是有效的。 However, when we add or delete data from the DataGrid ItemsSource, something goes awry, and TextChanged doesn't seem like it is called (or at least not called at the same time).但是,当我们从 DataGrid ItemsSource 添加或删除数据时,出现了问题,并且 TextChanged 似乎没有被调用(或者至少没有同时被调用)。 MyText can be one value, and the Inlines either blank or a different value. MyText 可以是一个值,而 Inlines 可以是空白或不同的值。

I think that the place to build the Inlines is NOT during MyTextChanged, but maybe when the rendering of the Control starts.我认为构建内联的地方不是在 MyTextChanged 期间,而是在控件的渲染开始时。 I've also tried when the DataContextChanged, but this does not help.我也尝试过 DataContextChanged 时,但这并没有帮助。

In my constructor, I have在我的构造函数中,我有

   this.myTextDescriptor = DependencyPropertyDescriptor.FromProperty(
        MyTextProperty, typeof(MyTextBlock));
    if (this.myTextDescriptor != null)
    {
        this.myTextDescriptor.AddValueChanged(this, this.MyTextBlock_MyTextChanged);
    }

corresponding to a dependency property I have in the class对应于我在 class 中的依赖属性

    public string MyText
    {
        get { return (string)GetValue(MyTextProperty); }
        set { SetValue(MyTextProperty, value); }
    }

    public static readonly DependencyProperty MyTextProperty =
        DependencyProperty.Register("MyText", typeof(string), typeof(MyTextBlock));

    private readonly DependencyPropertyDescriptor myTextDescriptor;

Update: If it is any kind of clue, the problem DataGrid cells seem to be the ones that are off-screen when the addition or removal happens.更新:如果有任何线索,问题 DataGrid 单元格似乎是在添加或删除发生时不在屏幕上的单元格。 I also tried OnApplyTemplate, but that didn't help.我也尝试过 OnApplyTemplate,但这并没有帮助。

Update2: Perhaps a better solution might be to create bindable inlines? Update2:也许更好的解决方案是创建可绑定的内联?

DataGrids virtualize their content, so if a row is not visible it will not be loaded. DataGrids 虚拟化它们的内容,因此如果一行不可见,它将不会被加载。 That being the case, have you tried also rebuilding the inlines when the Loaded event fires?既然如此,您是否尝试过在Loaded事件触发时重建内联?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM