Databinding: SyntaxEditor.Document.Text-Property is initialized from null to String.Empty ?

SyntaxEditor for WPF Forum

Posted 7 years ago by Christel
Version: 12.1.0562
Avatar

Hi,

in the user control that uses Syntaxeditor I use a Dependency Property (DP) 'Code'  as a wrapper for setting an getting the Syntaxeditor current text.

This DP is used from other usercontrols to bind a modelview-property to the 'Code' and therefore to the contens of syntaxeditor.

The DP as a wrapper calls Document.SetText  in order to do databinding on the contents of an SE

		#region DependencyProperty 'Code'

		public static readonly DependencyProperty CodeProperty = DependencyProperty.Register("Code", typeof(string), typeof(BaseSyntaxEditor), 
		    new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, new PropertyChangedCallback(OnCodeChanged)));

		public string Code
		{
			// IMPORTANT: To maintain parity between setting a property in XAML and procedural code, 
			// do not touch the getter and setter inside this dependency property!
			get { return (string)GetValue(CodeProperty); }
			set { SetValue(CodeProperty, value); }
		}

		private static void OnCodeChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e	)
		{
			BaseSyntaxEditor baseEditor = sender as BaseSyntaxEditor;
			if (baseEditor != null)
				baseEditor.OnCodeChanged((string)e.OldValue, (string)e.NewValue);
		}

		protected virtual void OnCodeChanged(string oldValue, string newValue)
		{
			// TODO: Add your property changed side-effects. Descendants can override as well.
			if (editor.Document.CurrentSnapshot.Text != newValue)
				editor.Document.SetText(newValue);
		}
		#endregion DependencyProperty 'Code'

 In the xaml of the usercontrol, which provides the DP 'Code'

the SyntaxEditor is "embedded" like this:

<editor:SyntaxEditor Name="editor"
		 Margin="0,0,0,0" 									
		 UserInterfaceUpdate="OnSyntaxEditorUserInterfaceUpdate"
		 DocumentParseDataChanged="OnSyntaxEditorDocumentParseDataChanged"	
		 IsTextDataBindingEnabled="True"
		 CanSplitHorizontally="False"
		 Text="{Binding RelativeSource={RelativeSource FindAncestor, 
AncestorType={x:Type local:BaseSyntaxEditor}},Path=Code, Mode=TwoWay, 
UpdateSourceTrigger=PropertyChanged}"									 
		>
</editor:SyntaxEditor>

 

The ViewModel has a Property bind to the Code-DepProp of the Syntax Editor.

              <syntaxEditor:Editor Grid.Row="2"
                                   Grid.Column="1"
                                   Height="300"
                                   Code="{Binding Context.Gnr,
                                           ValidatesOnDataErrors=True,
                                           Mode=TwoWay,
                                           UpdateSourceTrigger=PropertyChanged,
                                           }" />

 When the ViewModel is created, the Property value (here Context.Gnr), which bindingpath belongs to the DP 'Code' , is null.

As soon the syntaxeditor is shown, the property with null-value is set to String.Empty by SyntaxEditor, although I'm sure to 99,9%, that no code or the model itself set the value to String.Empty.

I can see that when debugging and set a breakpoint in the 'OnCodeChanged' method.

Could it bee, that the SyntaxEditor initializes its text.property with an empty string and therfore the DP 'Code' is set to an empty string, which itself leads to set the model-property from null to an empty string?

The problem is, that the modelview and as a result the model itself is changed ( as the property changed from null to empty string) and the user control, when closing, will save something, although nothing has really changed.

Did I something wrong with integrating the syntaxeditor in other user controls or with implementing the DP 'Code'?

Help is really appreciated!

Cheers, Christel

Comments (1)

Answer - Posted 7 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Christel,

Yes, you are correct.  SyntaxEditor will change its document text to be String.Empty if a null value is passed.  I would recommend you start your own property with a default of String.Empty instead of null to avoid the change notification.


Actipro Software Support

The latest build of this product (v2019.1 build 0683) was released 1 month ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.