views:

22

answers:

1

Hello everyone, my problem is the following :

I have defined a TextBox as a child of a ToolBar in a ResourceDictionary (x:Key MyToolbar). When my application loads, it places the ToolBar correctly inside the Window frame, along with its TextBox. So far, so good.

Of course, I'd like that very TextBox to be two-way databound to some objects' properties that are NOT defined in any ResourceDictionary.

More precisely, when the TextBox is all set in the correct window frame, and then, after the “Open” command a certain file is loaded, a Deserializer builds DesignerCanvas object using values from out of that file, in my case it is a string “Token” CLR property of a class that implements INotifyPropertyChanged.

Here some simplified code snippets. I will leave many blanks for clarity’s sake:

Class DesignerCanvas : INotifyPropertyChanged

{

  Private string m_token;

  Public string Token

  {

     Get{….

     Set{ if (value…)

     OnPropertyChanged(“Token”);

  }

  //notice there is no Constructor other than the default one

}

And on the XAML side I have something like this:

<ToolBar x:Key=”MyToolbar…..

    <TextBox …

Now, my two goals are: to have the “static” TextBox resource on my toolbar pick up the values of the DesignerCanvas’ “Token” property as soon as the property changes (i.e. gets a value for the first time, basically), and similarly, and more importantly, I wish to make it possible to have the DesignerCanvas read the values I could put in manually into the TextBox and fill its Token Property with that user-input text (I think I will opt for the TextBox’ LostFocus Event as a trigger for the string value being passed/bound onto the DesignerCanvas’ “Token” Property).

Anyway, I’m not sure how to set up a perfect two-way (or two-way-like) DataBinding between the TextBox' Text property and the DesignerCanvas' Token Property, since we have one static resource (I’m not sure if static is the correct word), and another dynamic runtime object (again not sure if runtime or dynamic are the words).

How do I achieve this? Do I absolutely need to register a “Token”- DependencyProperty in DesignerCanvas? Do I absolutely need to have a XAML for the DesignerCanvas defined somewhere (for example in my Window1.xaml or a dummy s:DesignerCanvas resource along with the TextBox)?

Help appreciated!

A: 

Have you tried databinding with your textbox...

<TextBox Text="{Binding Path=Token, Mode=TwoWay}" />

...then when your app loads and places the ToolBar in the window frame, make sure that it also sets the DataContext property of the ToolBar to the instance of your DesignerCanvas class?

Scott