views:

549

answers:

2

I am having a performance problem binding a large string to a a TextBox in WPF.

In the view I am binding a TextBox's Text property to the view model's Output property which is a StringBuilder.

View:

<TextBox Text="{Binding Output, Mode=OneWay}" IsReadOnly="True"/>

ViewModel:

    public StringBuilder Output
    {
        get { return _output; }
    }

As the text in the StringBuilder gets larger the performance of the binding degrades.

What's a better way to do this using MVVM?

A: 

I can't really imagine why the performance of the binding would be slow because it is simply displaying what's in the StringBuilder. However, the first thing that comes to my mind is how you're adding to the StringBuilder. Appending, removing, replacing, or inserting characters into the StringBuilder may be what's giving you performance issues.

I don't really know what kind of string you're building or what the requirements are, but you may need to use a different structure.

+3  A: 

One possible way of getting around delays in databinding is to use asynchrnous binding. You can do this by setting IsAsync property of your binding object :

This will of course not solve the problem of the binding taking a long time but will stop the UI from freezing whilst it does the binding.

You can also use priority binding to show a cut down version of the text (which is quick to load) whilst the larger text item is loaded. Priority binding is described on msdn - >http://msdn.microsoft.com/en-us/library/ms753174.aspx.

mattythomas2000