Hi,
I'm trying to bind the Xaml property of the RichTextBox:
<RichTextBox Xaml="{Binding Notes, Mode=TwoWay}"/>
and here is the error in Expression Blend:
"Value does not fall within the expected range."
Answer: 1
you can bind to dependencyproperties only
Answer: 2
According to Reflector, Xaml is a dependency property:
// Assembly System.Windows, Version 2.0.5.0 |
Location: | C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\Silverlight\v4.0\System.Windows.dll |
static RichTextBox() { XamlProperty = DependencyProperty.RegisterCoreProperty(0x37b9, typeof(string)); } |
Answer: 3
This considered a bug then? Wondered why I was getting Silverlight errors on binding the Xaml property using MVVM.
Answer: 4
Are there any workarounds to use binding for the RichTextBox?
Answer: 5
I really dont know how much I can help I do know that dependency property does not work with mvvm
Command binding is where you need to look into or using something that overrides dependency property
I myself am looking into using wcf for richtextbox
Answer: 6
rmtucker
According to Reflector, Xaml is a dependency property:
We don't document it as a dependency property, because you can't use it as a dependency property from user code. Note (from Reflector) that the XamlProperty field is private. That prevents you from using GetValue or SetValue directly for the Xaml property, and also has the side effect that a deferred operation like a binding declared by usercode can't use the dependency property system. I'm not specifically sure why the Xaml property was excluded, but the nature of its property system exposure makes it pretty clear that the behavior is By Design.
Answer: 7
I am not sure if this will help you but it is something you should consider
It is called n route from codeplex http://www.orktane.com/Blog/post/2010/04/28/nRoute-More-Wholesomeness-with-SL-4-and-NET-40.aspx
It can override dependency properties which is interesting
By the way wpf does not work verywell with mvvm
either so that's how I figured it out
WPF has flow documents that work with command bindings
Answer: 8
the only option i see now , is to implement the loaded event. and set there the xaml property.right ?
Answer: 9
This is my solution: Create a new class derived from RichTextBox and implement a new XamlProperty.
Code (VB.NET)
Public Class myRichTextBox Inherits RichTextBox Public boundXAMLProperty As DependencyProperty = _ DependencyProperty.Register("boundXAML", _ GetType(String), GetType(myRichTextBox), _ New PropertyMetadata(Nothing, New PropertyChangedCallback(AddressOf OnXamlChanged))) Public Property boundXAML() As String Get Return GetValue(boundXAMLProperty) End Get Set(ByVal value As String)
SetValue(boundXAMLProperty, value) End Set End Property Public Sub OnXamlChanged(ByVal d As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs) If boundXAML <> Xaml Then Me.Xaml = IIf(e.NewValue Is Nothing, _ "<Section xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'/>", e.NewValue) End If End Sub Private Sub myRichTextBox_ContentChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.ContentChangedEventArgs) Handles Me.ContentChanged boundXAML = Xaml End Sub End Class
Usage (XAML)
<myRichTextBox boundXAML="{Binding ...}"/>
Answer: 10
hi pnacci,
this code can only be used for loading the value .
what if the contents inside the richtextbox changes ? the contents shall not be updated to the "boundXAML" property.
what did you do to cater that?
Answer: 11
Hi jimijani1982,
myRichTextBox_ContentChanged() event will do the job.
I found an error in "OnXamlChanged" event, this is correct code:
Public Class myRichTextBox Inherits RichTextBox Public boundXAMLProperty As DependencyProperty = DependencyProperty.Register("boundXAML", GetType(String), GetType(myRichTextBox), New PropertyMetadata(Nothing, New PropertyChangedCallback(AddressOf OnXamlChanged))) Public Property boundXAML() As String Get Return GetValue(boundXAMLProperty) End Get Set(ByVal value As String) SetValue(boundXAMLProperty, value) End Set End Property Public Sub OnXamlChanged(ByVal d As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs) If boundXAML <> Xaml Then Me.Xaml = IIf(e.NewValue Is Nothing Or e.NewValue = "", "<Section xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'/>", e.NewValue) End If End Sub Private Sub myRichTextBox_ContentChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.ContentChangedEventArgs) Handles Me.ContentChanged boundXAML = Xaml End Sub End Class
Answer: 12
thanks.
Answer: 13
This worked for me? Sweet =)
Here is the C#:
public class CustomRichTextBox : RichTextBox { public static readonly DependencyProperty BoundXamlProperty = DependencyProperty.Register("BoundXaml", typeof(string), typeof(CustomRichTextBox), new PropertyMetadata(null, new PropertyChangedCallback(OnXamlChanged))); private static void OnXamlChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { CustomRichTextBox view = d as CustomRichTextBox; view.Xaml = (string)e.NewValue; } public string BoundXaml { get { return (string)GetValue(BoundXamlProperty); } set { SetValue(BoundXamlProperty, value); } } }
Then the Xaml was just this:
<YourNamespaceHere:CustomRichTextBox BoundXaml="{Binding PostText}" />
1 comment:
If set Xaml property in code-behind its working fine.
Post a Comment
Send us your comment related to the topic mentioned on the blog