views:

23

answers:

1

I'm currently trying to convert a wpf form with several textboxes on it to RichTextBoxes to allow for better editing functionality. The original textboxes were all linked to an underlying storage class using databinding to a data context upon navigation to the page.

public class Storage
{
  public String IntroFormText { get; set; }
  public String MainFormText { get; set; }
}

public partial class DataEntryPage : BasePage
{
  private Storage _storage { get; set; }

  public override NavigatingTo(object data, object savedState)
  {
    DataContext = _storage;
  } 

}

Originally, the bindings in the text box would have simply gone through and linked to the Storage class via the data context like so.

<TextBox
  Name="IntroText"
  Text="{Binding IntroFormText}
  SpellCheck.IsEnabled="True" />

This would then link via the data binding to _storage.IntroFormText automatically. To allow for two way data bindings in the RichTextBoxes, a custom implementation based on the ideas in this article was used. The problems come when trying to link the data bindings to the properties in the Storage class. With the RichTextBoxes the following declaration is used:

<Utils:BindableRichTextBox
  x:Name="IntroductionText"
  Document="{Binding {Path=IntroFormText, ElementName=dataEntryPage, Mode=TwoWay, Converter={StaticResource StringToXamlConverter}}"
  SpellCheck.IsEnabled="True" />

The idea is then to overload the getters and setters for the relevant property, adding the following lines to the DataEntryPage class.

private static readonly DependencyProperty introProperty = DependencyProperty.Register("IntroFormText", typeof(string), typeof(Storage));

public string IntroFormText
{
  get { return (string) GetValue(DocumentProperty); }
  set { SetValue(DocumentProperty, value); }
}

The problem I have here though, is that the property it needs to be linked with, resides in the Storage class, and NOT in the DataEntryPage class. Is there any way to get the DependencyProperty to effectively bind to the Storage class properties (as the TextBox does automatically), or am I going to have to write some kind of hacked together replacement for this behavior? I guess I need to overload the getters / setters for the Storage.Properties class inside of the DataEntryPage class to allow it to point at the DependencyProperty properly?

Due to the nature of the system, removing the data context bindings system isn't really an option unfortunately (plus I don't have permission to alter the design that heavily!). Hopefully though, this might make sense to someone who's run into the issue before!

+1  A: 

If you're already using a control that sets up the Document property as a DependencyProperty for you there isn't a need for you to declare another one for the Binding source. Only the Target of a Binding (in this case Document) needs to be a DP. If the control is written correctly you should be able to use the same Binding as with the TextBox, with the assumption that the content of that string will provide appropriate input to your StringToXamlConverter to produce a FlowDocument.

I'm hoping this isn't your actual code but if it is, your DP declaration (that you shouldn't need in this case) has numerous errors. DPs need to have a very specific format for their declarations to work properly. It's usually best to use a code snippet to generate it for you, especially if you don't have a solid understanding of how all the parts relate to each other.

John Bowen
This indeed isn't my exact code, more a summary / subset of a rather larger code base. In the end, it proved simpler to move away from DataBindings after further discussions. I see where you're coming from with the DependencyProperty stuff, I think at the end of the day I simply don't have a good enough understanding of it to use it truly neatly and effectively enough for this project. The idea intrigues me though, and its definitely something I'll be looking into more in the future! A decent article on custom DP material would be good to find though for future reference! :)
Smallgods
The MSDN article (http://msdn.microsoft.com/en-us/library/ms752914.aspx) is actually a pretty good overview. I'd stick to just the first few sections through "Setting Property Values" to get started.
John Bowen