WPF DependencyProperty: how to specify the OPPOSITE action of PropertyChangedCallback? - wpf

I'm adding a custom string DependencyProperty to a WPF UserControl that will be bound to a string property in my business object that contains rtf.
My PropertyChangedCallback works: it contains a snippet of code to use the e.NewValue rtf string to programmatically do a range.Load() on the RichTextBox nested in my UserControl, feeding it the rtf, when the DataContext changes or the business object's rtf string property changes (via INotifyPropertyChanged).
But what, or where, is the opposite callback to go back the other direction? When the inner RichTextBox loses focus after the user types/pastes their rich text, I need to run a mirror-image snippet of code to do the range.Save() to an rtf string, and that string needs to be written to the string property of the business object at that moment.
I am coming from Winforms, so I'm looking for the WPF analog of the Format/Parse pair that I'm accustomed to for round-tripping.

this.SetCurrentValue(MyDependencyProperty, range.Save());
this being the UserControl that owns the DP.
Also make sure your binding Mode=TwoWay.

Related

How to bind WPF-Datagrid Control in Editor

I am coming from programming with WinForms and now start to change into WPF. In WinForms I could easily bind a DataGrid before Runtime, to adjust the columns without coding everything.
The WPF-Datagrid has the property "ItemsSource", but I don't understand how to bind it in editor. I have already a DataSource which refers to a SQL-Database, but it will not be shown in the property window.
How to do this?
Screenshot
you have to expose your collection as a public property
make it visible in the xaml (namespace include)
set up your DataContext correctly
create the binding in XAML and set up the columns, like here
If you want to see the changes in your collection online, you have to use ObservableCollection<>

Advantage of Binding?

I am not sure that I fully understand the advantage of binding. For example, if I want to bind a string value to a TextBlock I need to do the following:
Create a class that extends INotifyPropertyChanged
Add a string to that class (say: MyString)
Extend the set method for MyString so that it calls another method (say: OnPropertyChanged)
Create the OnPropertyChanged method to call the PropertyChangedEventHandler event
Then I need to create a new instance of the class, set my TextBlock.DataContext to point to that class, and finally add the XAML bit for the binding.
Can someone explain the advantage of this over simply setting:
TextBlock.Text = MyString;
Thanks!
Any changes to MyString won't be automatically reflected in your UI.
Your code behind will be littered with "when this event occurs, update these pieces of data", so you'll essentially be writing your own messy data binding logic for each and every view.
The advantage is that you can both change and display the value in multiple places, without having to update some method to add another TextBlock assignment each time the value changes. Any new display control just binds itself to the property, the rest is automatic.
Now if you really just set the value in one place and show it in one control, then you're right, there's not much point.
The gain of using Data Binding isn't particularly noticeable for a TextBlock binding to a static string.
However if the value of MyString changes during application runtime it becomes much more useful - especially in a case where the object that owns that property is unaware of the TextBlock. This separation between UI and the underlying data layer can be created using a design pattern such as MVVM.
Data Binding is also useful for more complex properties such as Items in a ListBox control. Just bind the ListBox.Items to a property that is of type ObservableCollection and the UI will automatically update whenever the content of that collection changes.

Two-way-like binding between a Resource's property and a runtime object property

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!
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?

Can MVVM Usercontrols have property defined in codebehind?

I have a WPF user control ...which is in MVVM. The user control(which contains a listview) need data from the page (where it is included). I have to set a property to get this data input. Will this comply with MVVM...if not, what is the way for the same?
I'm afraid this won't be correct in MVVM design pattern. try to stick to your view model to define properties. Why don't you consider moving that property to control's vm?
Use an ObservableCollection rather.
ObservableCollection<myModel> myOC = new ObservableCollection<myModel>();
where myModel is a class that has to be constructed transforming your columns in the DataTable to Properties.
In your MainViewModel, loop through the DataReader and create myOC out of it.
Now bind myOC to a ListView in your page.
The DataTemplate of ListView should be a view(UserControl) drawing data from a ViewModel constructed out of myModel
But your UserControl has the entire ListView inside. If that is on purpose, then let me know the entire design to give a better idea.

DependencyProperty and DataBinding?

In WPF:
Can someone please explain the relationship between DependencyProperty and Databinding?
I have a property in my code behind I want to be the source of my databinding.
When does a DependencyProperty (or does it) come into play if I want to bind this object to textboxes on the XAML.
The target in a binding must always be a DependencyProperty, but any property (even plain properties) can be the source.
The problem with plain properties is that the binding will only pick up the value once and it won't change after that because change notification is missing from the plain source property.
To provide that change notification without making it a DependencyProperty, one can:
Implement INotifyPropertyChanged on the class defining the property.
Create a PropertyNameChanged event. (Backward compatibility.)
WPF will work better with the first choice.
What is the DependencyProperty?
The DependencyProperty class is one of the most important design bases hidden deep in the .Net Framework WPF.
This class is protected by sealed from the .NET Framework.
This property differs from the one-dimensional general property in that it not only stores field values, but also takes advantage of the various functions provided within the class.
Most importantly, there is a full foundation for data binding. You can also send notifications whenever you bind something.
DependencyProperty
Wpf Xaml Binding
It's already a late answer, but I'll introduce the results of my research.

Resources