[Feature Request] Make TextEditor.Text a DependencyProperty so you can bind a Property to it
See original GitHub issueIf you try to add a Binding to TextEditor.Text, the program fails with
Exception thrown: 'System.Windows.Markup.XamlParseException' in PresentationFramework.dll
Additional information: A 'Binding' cannot be set on the 'Text' property of type 'TextEditor'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject.
If you would be OK with post-processing, the easiest way would probably be to add the NuGet AutoDependencyProperty.Fody(https://www.nuget.org/packages/AutoDependencyProperty.Fody/) and decorate the property with [AutoDependencyProperty]
. This avoids the ‘code noise’ of Dependency Properties.
Issue Analytics
- State:
- Created 8 years ago
- Comments:7 (3 by maintainers)
Top Results From Across the Web
Two Way Binding to AvalonEdit Document Text using MVVM
Create a Behavior class that will attach the TextChanged event and will hook up the dependency property that is bound to the ViewModel....
Read more >Custom Dependency Properties - WPF .NET Framework
Implement the CLR "wrapper" property's get and set accessors to connect with the dependency property that backs it.
Read more >Make the Mask on TextEdit not prevent additional decimals ...
Hi, We have an application with a lot of TextEdits. Some of them are for input, some are readonly for results, and some...
Read more >How to implement a dependency property - WPF .NET
In this article This article describes how to implement a dependency property by using a DependencyProperty field to back a common language ...
Read more >How to implement Text property for RichTextBox control in ...
You can bind text with WPF RichTextBox content by implementing an extension class with Text property. Please refer the following code samples.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Making the
Text
property aDependencyProperty
would cause performance problems when editing large files: the only way to notify WPF that the DependencyProperty value has changed involves callingSetValue()
with the new value, which means the whole document text must be provided as an immutable string. When editing a 100 MB document, every key press would have to reallocate the whole 100 MB! Worse: those allocations go to the large object heap, so they can be only freed by a full gen2 GC.What AvalonEdit currently does is to use a mutable text buffer. The full
System.String
only gets allocated in theText
property getter, so the string allocation only occurs when necessary, and not on every key press.By the way, the WPF
TextBox
internally solves the same problem by using the WPF-internal typeSystem.Windows.DeferredReference
, which allows setting aDependencyProperty
to a value that is only produced on-demand. Unfortunately this solution is not available in the public WPF API 😦There are various solutions posted here but I don’t like any of these solutions. The proposed solutions do indeed cause the string to be reconstructed on every key strike to update the dependency property, leading to performance issues.
Binding to Document isn’t working properly, but even if it was, it means writing the MVVM ViewModel around a UI element which defeats the point of MVVM to be UI agnostic.
Honestly, I think the best option is to raise 2 events from the ViewModel, one to display text when it’s really needed, and one to get text when it’s really needed. Perhaps it doesn’t even need to be stored in the ViewModel at all.
Then, would it have been possible to derive TextEditor from the TextBox control to inherit its base features?